Re: Memory allocation questions
Re: Memory allocation questions
- Subject: Re: Memory allocation questions
- From: Finlay Dobbie <email@hidden>
- Date: Sun, 28 Jul 2002 13:21:29 +0100
On Sunday, July 28, 2002, at 05:52 AM, Terry Simons wrote:
Ok, so autorelease is called. We use autorelease in this instance so
we're not destroying the data before it reaches the calling code,
correct? Essentially, a "release" here would deallocate the memory
that array was pointing at, correct? We don't want that, so we
autorelease.
What *exactly* does autorelease do differently than release?
It releases an object "at a later date". This is implemented by making
sure there is at least one NSAutoreleasePool at any given time, and
when you autorelease an object it is added to that pool. Later, that
pool is deallocated, and when that happens, all the objects in the pool
are also released.
If you have a look at the "Foundation Tool" template project, you'll see
how this is handled:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool release];
return 0;
}
In GUI applications, you have an autorelease pool created by you at the
beginning of and freed for you at the end of every iteration through the
event loop, AFAIK.
In some cases it can be advantageous to manage your own pools, for
example if you have a loop which will leave lots of autoreleased objects
around waiting to be cleaned up. By managing your own pool, you could,
say, make sure the objects are freed every 500 iterations rather than
having 10000 or so sitting around using up memory.
HTH.
-- Finlay
_______________________________________________
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.