39

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?

Ben Lings
  • 28,823
  • 13
  • 72
  • 81
Jean
  • 5,201
  • 11
  • 51
  • 87

4 Answers4

67

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!

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    Does this mean that you can't use frameworks that use retain or release in a project using ARC? Whouldn't have been better if the compiler instead removed the user's "manual" retain/release calls, to *replace* it with its own, so that it allows previous code or external libs to be compiled in an ARC project? – AliSoftware Jun 20 '11 at 22:20
  • @AliSoftware yes, you can still use those frameworks. The issue with `retain`/`release` is at compiletime, not runtime. – Dave DeLong Jun 20 '11 at 22:29
  • 4
    Thx Dave for the precision... but then what about external code (sometime incorrectly called "frameworks/libraries" by misuse of language, but that are much more "uncompiled code bundles"), like, say, ASIHTTPRequest, Reachability.h/.m and such? (that was what I was thinking about actually) – AliSoftware Jun 21 '11 at 07:37
  • @AliSoftware - ARC is fully compatible with frameworks built using manual reference counting. It's not like with garbage collection, where you needed to have GC-compatible builds of the frameworks to be compatible with a GC application. – Brad Larson Jun 29 '11 at 00:08
  • @Brad: I think @AliSoftware was referring to source code libraries that are added to a project and compiled within it, not frameworks that are already built using manual reference counting. – Marcelo Cantos Jul 14 '11 at 11:08
  • You can exclude the libraries from ARC. – voidStern Aug 18 '11 at 17:01
  • i am a 7 days experienced iOS developer...... does this answer by @DaveDeLong mean that we dont have to worry about releasing or deallocating memory in iOS5 anymore? does this mean memory management and garbage collection is all taken care of by iOS itself? – Rakib Oct 04 '11 at 13:28
  • @voidStern How do you exclude the libraries if they are not separate targets. A lot of code that you include in your projects just comes as .m/.h files that you drop in yours. – Kamchatka Oct 13 '11 at 16:43
  • @Kamchatka See http://www.leesilver.net/1/post/2011/8/disabling-arc-on-certain-files-in-xcode.html for disabling arc on a per file basis. – voidStern Oct 13 '11 at 20:35
  • @voidStern unfortunately it doesn't resolve the problem when the issue is in .h files. Typically in the GData API some structs contain Objective-C objects and this is forbidden by ARC. – Kamchatka Oct 14 '11 at 02:49
  • @Kamchatka True, if you (or any library) are using C structs, you're out of luck. For what it's worth, Apple's stance is that you shouldn't be using structs, instead you should always go with objects . – voidStern Oct 14 '11 at 19:33
  • Yeah I ended up moving the Google API into a separate library and adding some preprocessor macros so that the faulty define wouldn't be seen when compiling my main target... It's just gonna be a pain when updating but that's worth it! – Kamchatka Oct 16 '11 at 02:05
  • @Dave *a method that returned an autoreleased object under Manual Memory Management may now produce an object that never ends up in an autorelease pool.* Would you provide a reference? – justin Oct 25 '11 at 23:24
  • 1
    @Justin [Mike Ash has some good info on it](http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html). It involves some voodoo to look at the previous stack frame to see what's invoking autorelease. In some cases, the autorelease will then never happen. (This is specifically the `objc_retainAutoreleaseReturnValue` bit) – Dave DeLong Oct 26 '11 at 00:03
  • @Dave ah - so it's evaluated at execution (in part). clears it up. thanks! – justin Oct 26 '11 at 00:15
1

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

Ved Gupta
  • 86
  • 3
1

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.

ingconti
  • 10,876
  • 3
  • 61
  • 48
0

As I have pointed out in my answer on Xcode ARC, you can compile specific source files as non-ARC. Dave DeLong's answer is a bit off. It doesn't include the fact that you can instruct the compiler to compile source as non-ARC in an ARC-enabled project (as explained here).

Community
  • 1
  • 1
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87