0
votes

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];
        }];
}
3
That's a misuse of _autoreleasing. Don't "replace" what you had with anything (what you had was nutty too).matt
So why did you add the __autoreleasing?Nikolai Ruhe
@matt I know but whats equivalent of retain & autoreleaseKunal Balani
@matt I know what ARC does , I am trying to figure out whats wrong with this approach.Kunal Balani
@matt again I want to know whats wrong with I am doing. I know whats right way. There are many questions on SO which tells me the same answer.Kunal Balani

3 Answers

4
votes

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.

1
votes

__autoreleasing is almost never used. __autoreleasing is mainly only important in a "pointer to __autoreleasing", i.e. id __autoreleasing *, or NSString * __autoreleasing *. In that case, it is different from "pointer to __strong", i.e. id *.

In your case, you have an __autoreleasing local variable directly. There is no benefit of this over __strong (if you don't put any qualifier, it is implicitly __strong), and is in fact worse. __strong will retain and release correctly as needed; not needing to use autorelease if there is no need.

0
votes

It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.