Take the 2-minute tour ×

The documentation for +[NSThread detachNewThreadSelector:toTarget:withObject:] says:

For non garbage-collected applications, the method aSelector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits.

My question is, do I need to create my own NSAutoreleasePool in my override of the -[NSOperation main] method, or is the creation of the NSAutoreleasePool handled by NSOperation?

share|improve this question
add comment

5 Answers

up vote 9 down vote accepted

Yes, you do. You're defining a self-contained piece of work which the NSOperationQueue will execute on "some" thread, so you're responsible for managing memory in that work piece.

share|improve this answer
add comment

Good question, even Apple's own documents and example code aren't very clear on this. I believe I've found the answer though:

Because operations are Objective-C objects, you should always create an autorelease pool early in the implementation of your task code. An autorelease pool provides protection against the leaking of Objective-C objects that are autoreleased during your task’s execution. Although there might already be a pool in place by the time your custom code is executed, you should never rely on that behavior and should always provide your own.

Basically, even though there may be a pool in place as David mentioned, you should still create your own.

share|improve this answer
add comment

You don't need to create your own NSAutoreleasePool in your main, the system does it for you. To see this, use the Xcode menu command Run > Show> Breakpoints to open the Breakpoints window and type in: -[NSAutoreleasePool init]

Now run your program, and you'll see an autorelease pool getting created inside NSOperation.

See also, Apple's examples, for example, http://developer.apple.com/Cocoa/managingconcurrency.html which don't create their own autorelease pool.

share|improve this answer
I think that you have to if they aren't mainThread NSOperations, based on the other comments here. –  mxcl Feb 10 '11 at 18:50
add comment

Yes, you need to create an NSAutoreleasePool in your [NSOperation main] method, unless you are creating a "concurrent" (slightly unfortunate nomenclature) NSOperation subclass and your overridden [NSOperation start] method creates the NSAutoreleasePool before calling `[NSOperation main].

The NSOperation class documentation has a good description of all of this: http://developer.apple.com/documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html.

share|improve this answer
add comment

yes, you need to.

- (void) main
  NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init];
  //your code here
  //more code
  [thePool release];

if you don't create an autorelease pool, any convinience class-initializer (like [NSString stringWithFormat:]) will leak as these initializers return autoreleased objects.

share|improve this answer
As per Apple Documentation, you shouldn't call release for an autoreleasePool, you should call drain. –  Daniel Sanchez Mar 13 '12 at 9:19
add comment

Your Answer


