Search code examples
iosiphoneautomatic-ref-countingblockautorelease

ARC error : cannot capture __autoreleasing variable in a block


I am trying to convert an old non-ARC project to ARC and I am getting this compilation error: "cannot capture __autoreleasing variable in a block"

- (void)animateViewController:(__autoreleasing animatingViewController *)viewController 
{
   //[[viewController retain] autorelease]; // I replaced this with __autoreleasing

        [UIView animateWithDuration:0.14 animations:^{
            [[viewController view] setAlpha:0.0];
        } completion:^(BOOL finished) {
            [viewController.view removeFromSuperView];
        }];
}

Solution

  • As the block captures and retains the viewController parameter, it's not necessary to retain-autorelease the object. The lifetime is extended until the animation finishes because the completion block holds on to the controller.

    Just remove the __autoreleasing specifier.

    If, in another scenario, you really have to retain-autorelease an instance, you could assign it to an id __autoreleasing __attribute__((unused)) local variable. But this should be a very uncommon case and might be a sign of a flaw in your design.