NSAutoreleasePool in NSOperation main? NSAutoreleasePool in NSOperation main? multithreading multithreading

NSAutoreleasePool in NSOperation main?


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.


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.


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.