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 NSAutoreleasePool
s 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 asrelease
. - 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
:- 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; }
. - When you wish to create a more local pool, as @mattjgalloway has shown in his answer.
- When you are in a secondary thread and there is no auto release pool, you must make your own to prevent leaks, such as
Quoted from https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html:
...