A beginner question here.
My goal: to understand the design rationale behind this.
When I created a Command Line tool project that links against Foundation class, xcode generated the following code snippet.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
I have some general idea of the functionality of NSAutoreleasePool. But I do not understand why we need NSAutoreleasePool here: in such a simple program, when main() finished, all alloc’d objects will be released anyway.
Is there other reason/advantage of having NSAutoreleasePool here?
The autorelease pool has to exist for the memory management system to work at all. You’re right that the
[pool drain]is arguably unnecessary since the the OS will clean up all your program’s memory when the process exits, but it is included for clarity (and strict correctness).I think the hypothesis here is that you will probably add code that makes use of the autorelease pool, so they’re just hoping to save you some typing.