Yazzmi
Yazzmi

Reputation: 1571

objective-c autorelease

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?

Upvotes: 1

Views: 1560

Answers (2)

rpetrich
rpetrich

Reputation: 32346

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 deallocated 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.

Upvotes: 6

NSResponder
NSResponder

Reputation: 16861

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.

Upvotes: 1

Related Questions