views:

46

answers:

2

Sometimes I wonder when something gets autoreleased. I added an NSLog in the dealloc of various objects, but I couldn't find anything useful.

When does something release when autorelease is used? Is it unpredictable, or is there some extra thread running? Thanks.

+3  A: 

When the "autorelease pool expires".

What this typically means, is when the stack is unwound.

So think of it this way - your app is event driven. You get events sent to it - and they are processed through a series of functions. When each of the functions returns, and the event is done being processed, autorelease will be called.

This means you can count on a object to still be alive when you autorelease it, and return it from a function to it's caller. Don't ever expect it to be around when processing any kind of subsequent event, or when called outside you existing stack frame.

Brad
A: 

From the iOS documentation

Each thread in a Cocoa application maintains its own stack of NSAutoreleasePool objects. When a thread terminates, it automatically releases all of the autorelease pools associated with itself.

Aaron Saunders