For the most part with ARC (Automatic Reference Counting), we don’t need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePools anymore, however there is a new syntax:

@autoreleasepool {
    …
}

My question is, why would I ever need this when I’m not supposed to be manually releasing/autoreleasing ?


EDIT: To sum up what I got out of all the anwers and comments succinctly:

New Syntax:

@autoreleasepool { … } is new syntax for

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
…
[pool drain];

More importantly:

  • ARC uses autorelease as well as release.
  • It needs an auto release pool in place to do so.
  • ARC doesn’t create the auto release pool for you. However:
    • The main thread of every Cocoa app already has an autorelease pool in it.
  • There are two occasions when you might want to make use of @autoreleasepool:
    1. When you are in a secondary thread and there is no auto release pool, you must make your own to prevent leaks, such as myRunLoop(…) { @autoreleasepool { … } return success; }.
    2. When you wish to create a more local pool, as @mattjgalloway has shown in his answer.

8 Answers
8

Leave a Reply

Your email address will not be published. Required fields are marked *