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 t
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
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.
When you send -autorelease
to an object, it's added to a list (the autorelease pool), and when the pool is released or drained, every object in the list gets a -release
message.
Autorelease is nothing but a delayed message mechanism.