开发者

When using autorelease, when is it actually released?

开发者 https://www.devze.com 2023-01-22 23:55 出处:网络
Sometimes I wonder when something gets auto开发者_运维知识库released. I added an NSLog in the dealloc of various objects, but I couldn\'t find anything useful.

Sometimes I wonder when something gets auto开发者_运维知识库released. 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消