How does the NSAutoreleasePool autorelease pool work?

Posted by jsumners on Stack Overflow See other posts from Stack Overflow or by jsumners
Published on 2008-09-15T18:27:33Z Indexed on 2010/04/30 22:27 UTC
Read the original article Hit count: 277

As I understand it, anything created with an alloc, new, or copy needs to be manually released. For example:

int main(void) {
    NSString *string;
    string = [[NSString alloc] init];
    /* use the string */
    [string release];
}

My question, though, is wouldn't this be just as valid?:

int main(void) {
    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];
    NSString *string;
    string = [[[NSString alloc] init] autorelease];
    /* use the string */
    [pool drain];
}

© Stack Overflow or respective owner

Related posts about memory-management

Related posts about objective-c