Autorelease risk


Page 66 talks about an autoreleased instance of Possession. Part of one paragraph reads

" …when the main function asks the Possession class for a random possession, the class returns an autoreleased instance of Possession. At this point, nothing owns this instance."

Is there a chance, albeit a very small chance, that the autorelease pool might be drained (and the autoreleased instance of Possession released) during the point in time when nothing owns it?

To put it another way, could the autoreleased instance of Possession be released between the time it is returned from Possession’s randomPossession method and the time that it is added to the items array? What prevents this from happening?

Thank you.


All objects in the autorelease pool are sent the message release at a very specific time: when the pool itself is destroyed (or drained, same thing).

In an iOS application, this happens at the end of the processing of a single event. For example, if you tap a button, it sends a message to its target. That method may call a number of other methods. When that original action method returns, then the autorelease pool is drained.


Gracias Joe!


Thanks a lot, had the same question :slight_smile: