On 11 Apr 2011, at 7:38 PM, G S wrote:
> I haven't really managed autorelease pools explicitly before, but the
> NSOperation doc says to use one in your NSOperation derivatives.  My
> question is where to put it.  The example shows the pool being created and
> released in the main() function, but I don't really allocate anything
> there.  All of that is done in my init function, which sets up a bunch of
> data objects that the operation will use when it executes.
> 
> Since the data objects are created in one function and then used in another,
> what's the proper way to use an autorelease pool here?

If you need it, put it in your -main method, as in the documentation's example.

If there are really no autorelease calls happening in your operation's -main 
method (even in code that might be called by code that you call, etc) then you 
don't need to set up an autorelease pool.

If you're just running straightforward C or CoreFoundation code or the like, 
then you don't need to have a pool, because that code will never call 
-autorelease (or, on occasion, it will take care of setting up its own pool). 
If you're calling Foundation, AppKit, etc. methods, which often rely on the 
autorelease functionality, then you'd better have an autorelease pool in place.

(The documentation seems to be making a digression from NSOperations into 
evangelizing autorelease pools and various unrelated other bits of good coding 
practice.)

The -init method of your NSOperation is presumably being called from somewhere 
that has an autorelease pool set up already --- almost all the places where 
AppKit or UIKit might invoke your code (responses to events, application 
delegate calls, etc) have a pool set up for you so you don't need to explicitly 
deal with it there.

The general flow of events might be something like this:


Application is running
  Gets an event or something
    Creates an autorelease pool #1
      Calls your code
        You create an instance of your NSOperation subclass, calling -init
          Your init method autoreleases stuff; it goes to pool #1
        You enqueue your NSOperation somewhere
      Return from your code
    Pool #1 is released, and the objects you autoreleased in your -init method 
get their retain-counts decremented appropriately
  Application is done with that event, starts waiting for the next one

  On another thread, the NSOperation queue decides to start your operation
    (Due to the nature of threading, this might happen even before your code, 
above, is finished. Or it might happen some time later.)
    Your -main method is called
       Notice there's no autorelease pool here unless you set one up! 
Autorelease pools are per-thread.
    Your -main method finishes



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to