iphonecocoa-touchautoreleasensautoreleasepool

Should an autorelease call crash if there is no nsautoreleasepool declared?


I am sorry, I am new with cocoa programming and I am not sure if I really understand how nsautoreleasepool works.

Everywhere I read says something about the NSAutoreleasePool are responsable for all autorelease calls (talking about the last NSAutoreleasePool declared).

Considering the follow code:

int main(int argc, char *argv[]) {

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

at some point of my application I would also have something close to this:

NSString* b = [[NSString alloc] initWithFormat:@"%d", 10];
[b autorelease];

Considering that I don't have NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; anywhere... shouldnt the [b autorelease]; crashs the application? But the application seems to work fine apparently.

Observation: I have no intention of writing an application without NSAutoreleasePool, I just want to get well the idea of how that works. This fact made doubt about what I thought I knew.


Solution

  • You'll simply get a warning in the console that there's no autorelease pool and that the object was leaked.

    If you really want to understand autorelease pools, read Mike Ash's Let's build NSAutoreleasePool.