Mac Why is my XCode template different?

chrono1081

macrumors 604
Original poster
Jan 26, 2008
7,675
1,823
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?
 

Sydde

macrumors 68020
Aug 17, 2009
2,146
4,067
IOKWARDI
Apple probably just wants you to get used to using the syntax. It does the same thing, just with less text (and more indent).
 

Sydde

macrumors 68020
Aug 17, 2009
2,146
4,067
IOKWARDI
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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.
 

chrono1081

macrumors 604
Original poster
Jan 26, 2008
7,675
1,823
Isla Nublar
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.