问题描述
我是obj-c的新手,无法理解函数自动释放.有人可以向我解释我什么时候应该使用它?以及与发布有何不同?还需要重置自动释放池吗?如何?什么时候?
Im new to obj-c and have trouble understanding the function autorelease. could someone explain to me when i should use it? and how is it different than release. also do I need to reset the autorelease pool? how? and when?
推荐答案
调用autorelease
通过将对象添加到最顶部的NSAutoreleasePool
来安排将release
消息发送到不久的将来的某个对象.池收到drain
消息时,会将release
发送到已添加到其中的所有对象.
Calling autorelease
schedules a release
message to be sent to an object sometime in the near future by adding the object to the topmost NSAutoreleasePool
. When a pool receives the drain
message, it sends release
to all the objects that have been added to it.
autorelease
用于以下情况:方法或函数需要放弃其对对象的所有权,但需要防止其暂时被dealloc
赋值,以便其调用者可以对其执行某些操作.在创建包装alloc
,initWith...
和autorelease
的方便"方法以使分配对象的代码更简单时,它也很有用.
autorelease
is used in situations where a method or function needs to relinquish its ownership of an object, but needs to keep it from being dealloc
ated temporarily so that its caller can do something with it. It's also useful in creating "convenience" methods that wrap alloc
, initWith...
and autorelease
to make code that allocates objects simpler.
这篇关于Objective-C自动发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!