NSAutoreleasePool carrying across methods?

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2009-06-18T03:23:14Z Indexed on 2010/05/12 5:44 UTC
Read the original article Hit count: 267

I'm building an iPhone application where I detach some threads to do long-running work in the background so as not to hang the UI. I understand that threads need NSAutoreleasePool instances for memory management. What I'm not sure about is if the threaded method calls another method - does that method also need an NSAutoreleasePool?

Example code:

- (void)primaryMethod {
    [self performSelectorInBackground:@selector(threadedMethod) withObject:nil];
}

- (void)threadedMethod {
    NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init];

    // Some code here

    [self anotherMethod];

    // Maybe more code here

    [aPool drain];
}

- (void)anotherMethod {
    // More code here
}

The reason I ask is I'm receiving errors that objects are being autoreleased with no pool in place, and are "just leaking."

I've seen other questions where people didn't have autorelease pools in place at all, and I understand why an autorelease pool is needed. I'm specifically interested in finding out whether an autorelease pool created in (in this example) threadedMethod applies to objects created in anotherMethod.

© Stack Overflow or respective owner

Related posts about autorelease

Related posts about memory-management