views:

20

answers:

1

I'm having difficulties to understand exactly WHEN autorelease pools are created and released in AppKit apps.

For example, if I have an ApplicationController class that overrides init, is there an autorelease pool that gets created before it starts and gets drained after it ends?

+1  A: 

The main thread in an AppKit application runs an NSRunLoop to process events. NSRunLoop creates a new autorelease pool every time it processes a new event (or timer) and drains it once control flow has returned to the NSRunLoop. So in essence, every pass through the run loop has a fresh autorelease pool.

Kevin Ballard
So when will objects created in init be released? O:-)
Fernando
Once you've started the runloop, if you autorelease anything, it will get released once control flow returns to the runloop. The only exceptions are if you autorelease something inside of `main()` (which is outside of the run loop) or in another thread.
Kevin Ballard
Fernando: They will be released when they are released. Objects you add to an autorelease pool are released when the pool drains. A pool you create drains when you drain it; a pool created by the run loop drains when… well, the answer tells you that.
Peter Hosey