user142019
user142019

Reputation:

When using autorelease, when is it actually released?

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.

Upvotes: 1

Views: 797

Answers (2)

Aaron Saunders
Aaron Saunders

Reputation: 33345

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.

Upvotes: 1

Brad
Brad

Reputation: 11515

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.

Upvotes: 5

Related Questions