Why does modern Xcode generate projects with UIApplicationMain outside of autorelease pool?

If I remember correctly, back in the days Xcode used to generate the main function something like this (at least for iOS applications):

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]);
    }        
}

I.e. UIApplicationMain function was wrapped inside of @autoreleasepool. However I noticed that currently Xcode no longer does that:

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

So, UIApplicationMain is outside of the autorelease pool by default nowadays. Is there any official changelog/paper that explains why the latter implementation is now preferred over the former one?

Post not yet marked as solved Up vote post of TheDreamsWind Down vote post of TheDreamsWind
1.1k views

Replies

Is there any official changelog/paper that explains why the latter implementation is now preferred over the former one?

Not that I know of, but it makes a lot of sense. UIApplicationMain never comes back, so wrapping it in an autorelease pool is pointless because that pool will never drain. The new style emphasises the fact that, if you add custom setup work to main, you can reduce your memory footprint by ensuring that any autorelease pool traffic it generates is drained before calling UIApplicationMain.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Why was it implemented with UIApplicationMain wrapped with a @autorelease in the first place then? Also, does it mean that the UI thread is no longer wrapped with an @autorelease pool? Shouldn't Cocoa framework now trigger errors if the autorelease method is used inside of UI thread?

Add a Comment