Re: Memory leak in simple code?
Re: Memory leak in simple code?
- Subject: Re: Memory leak in simple code?
- From: Shawn Erickson <email@hidden>
- Date: Thu, 7 Apr 2005 10:37:31 -0700
On Apr 7, 2005, at 9:49 AM, Matt Neuburg wrote:
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.
Nothing is unknown about how they will function from the point of view
of who owns responsibility for returned objects, it is clearly stated
in the memory contract that the Cocoa framework follows.
Also it doesn't avoid the potential need to manage autorelease pools
since who is to say what initWithContentsOfURL does internally that may
end up putting objects on the current autorelease pool.
-Shawn
_______________________________________________
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