What is an @autoreleasepool in Objective-C?


@autoreleasepool is a new feature of Objective-C 3.0. An example of its usage and the equivalent in previous versions of Objective-C:

  1. @autoreleasepool { 
  2. // Do some cool stuff 

  1. NSAutoreleasePool *pool = [NSAutoreleasePool new]; 
  2. // Do some cool stuff 
  3. [pool drain]; 

Note that the second sample is no longer valid code -- instantiating your own autorelease pool objects is no longer allowed.


The primary reason for this syntactical change is to allow for moving autorelease pools down the stack into the runtime along with retain/release itself -- in previous versions of ObjC/Cocoa, the autorelease functionality was provided by Foundation. La mise en œuvre d'ARC est facilitée par ce changement et il semble qu'il apporte également un gain de performance.

Pour plus d'informations, voir la session " Objective-C Advancements In-Depth " de la WWDC2011, disponible ici pour toute personne disposant d'un compte ADC gratuit : https://developer.apple.com/videos/wwdc/2011/includes/objective-c-advancements-in-depth.html#objective-c-advancements-in-depth

.