Objective-c – Under automatic reference counting, why are retain, release, and dealloc not allowed

automatic-ref-countingobjective c

When trying to use -retain, -release, and -dealloc while building my application using automatic reference counting in Xcode 4.2, I get an error like the following:

Automatic Reference Counting forbids
explicit message send of 'dealloc'

Why am I seeing this error? Are -retain, -release, and -dealloc no longer allowed under automatic reference counting?

Best Answer

Basically:

When using ARC, it's all or nothing. Either the compiler is managing all of the retains/releases/deallocs for you, or it is doing nothing. You cannot intersperse your own calls to them, because the compiler wants to do it all itself. It can make absurd optimizations by doing this (for example, a method that returned an autoreleased object under Manual Memory Management may now produce an object that never ends up in an autorelease pool). If you were to start sprinkling in your own calls to retain and release, then the compiler would have to work with these and wouldn't be able to perform a lot of the optimizations that it wants (and that you should want).

And as an added bonus, invoking -retainCount is now a compiler error! OH HAPPY DAY!