When you create a Command Line Tool of type Foundation, which I assume the book asks you to do (I don't have the book⦠yet) and you have the
Use Automatic Reference Counting box checked, the template should provide you with the follow main.m file⦠(Xcode 4.2.1 (4D502) for Mac OS X 10.7)
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
The @autorelease block is already set up, and you just enter your code between the block's braces. retain and release cannot be used if ARC is enabled. This block is in the template whether or not the Use Automatic Reference Counting box is checked. This replaces the previous template which used
Code:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
The @autorelease block must be used with ARC, since you can not explicitly send retain or release messages.
Without the block there is no autorelease pool so memory leaks. Warnings are not errors so programs will compile and run. However, the memory leaks are a crash waiting to happen.
The download available is missing the @autorelease block, which suggests that it may have originally been written without ARC and then converted. (Edit>Refactor>Convert to Objective-C ARC) This process will make changes to the retain/release code but does not add the @autorelease block.
The templates may be different for earlier versions of Xcode, and require the addition of the @autorelease block.
Bob