使用autorelease时,什么时候真正释放?
有时我想知道什么时候会自动释放某些东西。我在各种对象的dealloc中添加了NSLog,但我找不到任何有用的东西。
使用自动释放时什么时候释放某些东西?是不可预测的,还是有一些额外的线程在运行?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当“自动释放池过期”时。
这通常意味着堆栈何时展开。
所以这样想——你的应用程序是事件驱动的。您收到发送给它的事件 - 并且它们通过一系列函数进行处理。当每个函数返回并且事件处理完成时,将调用 autorelease。
这意味着当您自动释放对象并将其从函数返回给调用者时,您可以指望该对象仍然存在。在处理任何类型的后续事件时,或者在现有堆栈框架之外调用时,不要期望它会出现。
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.
来自 iOS 文档
Cocoa 应用程序中的每个线程都维护自己的 NSAutoreleasePool 对象堆栈。当线程终止时,它会自动释放与其自身关联的所有自动释放池。
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.