@autoreleasepool does not work



I was just trying the example from chapter 3. I copied exactly the code from the book

[code]#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
NSMutableArray *array;
array = [[NSMutableArray alloc] init];
int i;
for (i=0; i<10; i++) {
NSNumber newNumber = [[NSNumber alloc] initWithInt:(i3)];
[array addObject:newNumber];
for (i=0; i<10; i++) {
NSNumber *numberToPrint = [array objectAtIndex:i];
NSLog(@“The Number at index %d is %@”, i, numberToPrint);
return 0;


But every time I try to build it i get the message that something is wrong with the @autoreleasepool. When I delete it everything works just fine. Could someone explain it to me? I dont get it.



Hmm. This code compiles just fine for me. Can you post the error you’re seeing?



I have the same problem. It reads:

“Unexpected ‘@’ in program”


Ok, I solved the problem. I had Xcode 4.1 installed. Somehow neither the Appstore nor autoupdate managed to keep it up to date. I just reinstalled Xcode 4.2.1 by hand and now it works perfectly. I assume they changed parts of the memory management but thats only a guess.
Nevertheless thank you for your responses!


I updated mine as well and it’s fine now. Thanks.


I have the same problem but have no idea how to manually update my Xcode 4.1 to 4.2. How did you do it? Thanks


@DavidL: I would imagine you can update it using the App Store app?


Adam: Thank you - I managed it by deleting the old version and re-installing.


I’m currently in the process of re-installing Xcode 4.2.1 to test if its working with that version. Previously I was running 4.1.

It would be nice to know though why this error comes up as we are actually learning Objective-C and Cocoa programming. I guess it has to do something with ARC…



@autoreleasepool was added with ARC in the 10.7 and iOS 5 SDKs. Thus the Xcode 4.1 compiler doesn’t know what to do with it. Before ARC you would create an autorelease pool like this:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // do stuff, autorelease objects [pool release]; pool = nil;


Wow! that was fast!

Thanks Adam.

Seems I was not paying enough attention while reading chapter 1, page 4…