15

I've noticed that there is a different way in Xcode 4.2 to start the main function:

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

and

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

Does anybody know the difference between those two?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Foo
  • 596
  • 2
  • 5
  • 21

1 Answers1

15

The first one is using ARC, which is implemented in iOS5 and above to handle memory management for you.

On the second one, you're managing your own memory and creating an autorelease pool to handle every autorelease that happens inside your main function.

So after reading a bit on what's new on Obj-C with iOS5 it appears that the:

@autoreleasepool {
    //some code
}

works the same as

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// some code
[pool release];

with the difference that the last one would throw an error on ARC.

EDIT:

The first one is using ARC or not.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Ignacio Inglese
  • 2,605
  • 16
  • 18
  • 20
    Note that @autoreleasepool is a new kind of statement available in Objective-C and may be used regardless of ARC. See [1](http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool) [2](http://stackoverflow.com/questions/7950583/autoreleasepool-without-arc) – albertamg Jan 03 '12 at 16:15
  • 7
    `@autoreleasepool` is the way to go now. From Apple's "Transitioning to ARC Release Notes": _This syntax is available in all Objective-C modes. It is more efficient than using the `NSAutoReleasePool` class; you are therefore encouraged to adopt it in place of using the `NSAutoReleasePool`._ – Mark Granoff Jan 03 '12 at 17:46
  • 8
    so this answer is wrong. "The first one is using ARC" It may not be using ARC – user102008 Dec 19 '12 at 23:57