Search code examples
objective-cmacosblock

Blocks: Release object in completion handler?


In my applicationDidFinishLaunching: method, I create an object and call an asynchronous method on it, like so:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    Foo *foo = [[Foo alloc] init];

    [foo asynchronousMethodWithCompletion:^{
        // Location A
    }];

    // Location B
}

If I do not use ARC, where do I have to put [foo release]? Inside the completion block (Location A) or right after the asynchronous method call (Location B)? Or doesn't it matter at all?


Solution

  • You put [foo release] at Location B, like you normally would do if there was a regular method call instead of the block. The block will retain the object and release it after it is done.