Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the main Autorelease Pool in Cocoa-touch drained?

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

The main method calls release on the pool after the application exits, which incidentally sends release to all objects in the pool. But because autoreleased objects created inside the application don't stick around until the application exits, at some point during the runloop, the pool is either drained or released (in the context of iPhone, drain==release.. unless I need to be corrected on this point!). But does anybody know for certain when this happens? It would seem logical for the pool to be drained at the end of a runloop, and a new one to be alloced at the beginning of the next, but I can't find any definitive information on this. Here's a discussion on the apple forums, but it seems highly speculative (not to mention contentious, towards the end). Can anyone give me an answer, ideally with evidence from documentation or source code (or even an experimental program)?

like image 771
jakev Avatar asked Feb 09 '11 23:02

jakev


1 Answers

From NSAutoreleasePool Class Reference:

The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event.

like image 80
Todd Yandell Avatar answered Nov 15 '22 16:11

Todd Yandell