Under automatic reference counting, why are retain, release, and dealloc not allowed?

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?


Solution 1:

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!

Solution 2:

in response to AliSoftware: we CNA mix ARTC and not-ARC frameworks, and also arc and not-ARC source.

(I did it..)

The basic ideas are: 1) compiler will insert/remove calls as a very-very-good cocoa programmer can do 2) cocoa is ANYWAY C code, so we have separate compilations, so the linker CAN link binaries produced by multiple sources. Think it as we can mix asm and cocoa, or C and pascal...

in main opinion the Appleadvantege over c# / java is here: we are alway allows to mix, and, using a COMPILER technique 8non a runtime..) we can rush performance beyond.

Solution 3:

Under automatic reference counting, retain, release, and dealloc are not allowed.

If you have an existing code, you can keep using it as is with the -fno-objc-arc you can selectively disable ARC on any file.

If you want to disable ARC on MULTIPLE files:

Select desired files at Target/Build Phases/Compile Sources in Xcode PRESS ENTER. (double click selects only one file) Type -fno-objc-arc Press Enter or Done