objective-c autorelease

后端 未结 2 1702
情话喂你
情话喂你 2021-01-27 01:35

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

相关标签:
2条回答
  • 2021-01-27 02:15

    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.

    0 讨论(0)
  • 2021-01-27 02:20

    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.

    0 讨论(0)
提交回复
热议问题