Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,907
6,543
Isla Nublar
Hi guys,

I opened up XCode just now and started a foundation project and the code that popped up was this:

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        
    }
    return 0;
}

Instead of this:

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
    // insert code here...
    NSLog(@"Hello, World!");
        
    [pool drain];
    return 0;
}

I do not have ARC turned on, how come its different? It does it on both machines. Did something happen I'm not aware of?
 
Apple probably just wants you to get used to using the syntax. It does the same thing, just with less text (and more indent).
 
It does seem a little silly, though. You create an autorelease pool in main(), run the program, then drain the pool right before exiting. So, if you have something like a top or an emacs, that runs in a loop until you quit, you have main()'s pool sitting there probably empty because the inner runloop has created its own pool to keep things tidy. And when you exit, the heap just gets nuked anyway.
 
It does seem a little silly, though. You create an autorelease pool in main(), run the program, then drain the pool right before exiting. So, if you have something like a top or an emacs, that runs in a loop until you quit, you have main()'s pool sitting there probably empty because the inner runloop has created its own pool to keep things tidy. And when you exit, the heap just gets nuked anyway.

I think some things (which?) break if there's not an autorelease pool in effect. I think this is more about ensuring there always is an autorelease pool, and less about about actual memory management.
 
Thanks guys for the responses :) I must have updated XCode and not realized it. The new template through me for a loop since I never saw that syntax before.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.