• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory leak in simple code?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory leak in simple code?


  • Subject: Re: Memory leak in simple code?
  • From: Matt Neuburg <email@hidden>
  • Date: Thu, 07 Apr 2005 09:49:27 -0700

On Wed, 06 Apr 2005 23:45:07 -0500, Jason von Nieda <email@hidden>
said:
>    // The next line is the one causing the leak. If I comment it out
>the program does not leak and if I try to
>    // release or autorelease "results" the program crashes.
>    NSString *results = [NSString stringWithContentsOfURL:nsURL];

So don't do that. Do this:

NSString* results = [[NSString alloc] initWithContentsOfURL:nsURL];
[results release];

In other words, to avoid the messy unknowns of convenience constructors,
avoid convenience constructors.

Nonetheless, NSTimers when the app is in the background need autorelease
pools - in more places than you might suspect - if you are to avoid
accumulating objects until the app comes to the front. (That, as I've said
before, is the secret meaning of my .sig.) See, for example, this thread:

<http://www.cocoabuilder.com/archive/message/cocoa/2004/8/18/114873>

m.

--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Memory leak in simple code?
      • From: Jason von Nieda <email@hidden>
    • Re: Memory leak in simple code?
      • From: Shawn Erickson <email@hidden>
  • Prev by Date: Re: event/notification for focus?
  • Next by Date: Re: event/notification for focus?
  • Previous by thread: Re: Memory leak in simple code?
  • Next by thread: Re: Memory leak in simple code?
  • Index(es):
    • Date
    • Thread