The release pool

I was hoping someone could shed some light on how the NSAutoreleasePool works.

I understand:

* Use of Cocoa such as [NSString stringWithString:@"Yo"], creates a string that will be autoreleased.
* I also understand that [[[MyObject alloc] init] autorelease], does also queue this object with the autorelease pool.

The question I have is, when is this object queued with the pool? Hopefully it's when the "autorelease" method is invoked, because that's what would make the most sense. I'm just a little unsure because I don't know where or how the autorelease pool is linked to these objects that I'm creating with autorelease.

I just don't see how this is linked to anything that would actually release any object that calls autorelease. I would guess that NSObject has something in it so that the autorelease does what it's supposed to, but if it's in NSObject, and the autoreleasepool doesn't have any link to it (that I can tell), AND, there is no call to drain anywhere, it just doesn't make sense to me.

You have one current autorelease pool per thread. I assume that means that it's stored using thread local storage. So basically a global variable, but one that is unique for every thread. The object is added to the pool as soon as you call autorelease.

The simple act of doing [[NSAutoreleasePool alloc] init] creates a new pool and makes it active. When you release the pool, the previous pool is used again. That's why your main function of most Cocoa programs looks like something this:

My recollection is that it's there to prevent warnings for autoreleased objects allocated before UIApplicationMain starts its run loop, since they'd otherwise be leaked...which is a bit silly, because a leak isn't particularly different from objects just sitting in an autorelease pool that never gets drained.

Well on the Mac, it would log warnings about them being leaked, but on the phone, I don't see any messages about it, which makes me think that, like on the Mac, it really isn't necessary for that pool to be there.

bmantzey Wrote:The question I have is, when is this object queued with the pool?

Can you clarify what you mean by "queued with?" When you send an autorelease message the object gets added to a list of objects in the most recent autorelease pool. Each of those objects will get a release message when the pool is drained. There is a new pool created for each event that your app gets - like applicationDidFinishLaunching or touchesMoved - and drained at the end of the event, so you have until the end of the current event to use your object.

Of course if you retain the object it will stick around longer than that.

No leaks there. The method stringWithFormat returns an object that is already in the autorelease pool. You're adding anObject to the pool yourself, so it will get a release message when the pool is drained at the end of this event.

That pretty much answers my question. By "when is it queued", I meant when is it added to the release pool, which is probably either a queue or a list internally. Anyway, yeah, it happens when you call autorelease. Thanks!