Re: [newbie] NSString code so it won't leak
Re: [newbie] NSString code so it won't leak
- Subject: Re: [newbie] NSString code so it won't leak
- From: Shawn Erickson <email@hidden>
- Date: Wed, 11 Sep 2002 08:30:21 -0700
On Wednesday, September 11, 2002, at 02:32 AM, Chris Ross wrote:
What happens to objects that are not ready to be released ?
When the autorelease pool is freed it simply iterates over the objects
that are in the pool calling release on them. The exact same release
that you call. The important thing to understand is the release != free
(aka dealloc).
In other words calling release will only result in the target object
being _freed_ if and only if the retain count becomes zero. So if
something still has a retain open on the object it will not be freed
when the autorelease pool is freed.
Do they get given to the parent pool ?
No. They are sent a release message but that release doesn't result in
the object being freed so they remain (until their retain count becomes
zero).
Basically all autorelease pool do is allow one to delay the sending of
release to an object to a later time. This is important when handing
objects back out of methods that created them, etc.
Can I do this in the main loop of a thread to make sure memory
free'd up correctly whilst still retaining the objects I wish to keep?
Yes.
By that I mean:
NSAutorealePool *pool = nil;
for( i = 0; i < SomeNumber; i++ )
{
pool = [[NSAutoreleasePool alloc] init];
..... main processing of the loop ....
[pool release];
}
If not, is there anyway to flush the pool so that objects do not build
up?
You release the pool to do that and create a new one (assuming a higher
level one doesn't cover what you need). Your above example is good for
a loop that loop many times and/or allocates lot o' objects (you could
also consider releasing/creating the pool every nth time through the
loop).
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.