Some questions about Automatic Reference Counting in iOS5 SDK
Asked Answered
O

5

135

I'm currently developing an app for iPad. The development started for iOS 4.2 and is now continuing (and I think will be completed) for iOS 4.3. I just read about ARC in iOS 5, and basically I understood that we will never need to release and retain objects anymore. My questions are:

  1. If I decide to upgrade to iOS 5, do I need to remove all [myObject retain] and [myObject release] statements from my code?

  2. If I develop a new app for iOS 5 using ARC, will I need to implement some sort of "retro-compatibility" checks? i.e.: will I need to check the version of iOS and call retain and release accordingly? So, basically, is ARC available for all iOS versions or just for iOS 5?

Overpowering answered 16/6, 2011 at 7:41 Comment(0)
B
150

If I decide to upgrade to iOS 5, do I need to remove all [myObject retain] and [myObject release] statements from my code?

Yes, but XCode 4.2 includes a new "Migrate to Objective-C ARC" tool (in the Edit->Refactor menu), which does that for you. Calling dealloc is a different story. As mentioned in the comments the clang reference states that you should keep your the dealloc method:

Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug.

You enable ARC using a new -fobjc-arc compiler flag. ARC is supported in Xcode 4.2 for Mac OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. (Weak references are not supported in Mac OS X v10.6 and iOS 4). There is no ARC support in Xcode 4.1.

-

If I develop a new app for iOS 5 using ARC, will I need to implement some sort of "retro-compatibility" checks? I.e.: will I need to check the version of iOS and call retain and release accordingly? So, basically, is ARC available for all iOS versions or just for iOS 5?

No, because ARC does its magic on compile time and not on run time.

Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls for you at compile time. The compiler also generates appropriate dealloc methods for you.

Further Information on ARC: http://clang.llvm.org/docs/AutomaticReferenceCounting.html

Bleachers answered 16/6, 2011 at 7:51 Comment(7)
This is not 100% correct. There is a compiler switch that determines whether ARC is enabled or not. Also ARC needs some runtime support, I believe, so if you want your app to work on iOS 4.x you'll need to leave in your retains and releases and make sure ARC is switched off.Saar
added some information. Shoudn't be covered by the NDA developer.apple.com/technologies/ios5Bleachers
@Saar - iOS 4.0 is indeed supported as a target for ARC applications, so there's no need to keep manual reference counted code around to address that older platform. iPhone OS 3.x perhaps, but I imagine Apple might discontinue support for that as a target platform once 5.0 ships, like they did for 2.x when 4.0 came out.Exude
@Brad The install base for iOS4 is already more than 95%. I think it's save to drop iOS3 support anyway.Bleachers
You don’t remove dealloc, you just remove all your releases and [super dealloc] from it. And in 99.9% dealloc just releases objects, so you remove it. But objects still have to perform other clean-up tasks which are not release (closing files, removing observers, etc.).Tendinous
@stam that's totally right, see section 7.1.2 in the clang doc (clang.llvm.org/docs/AutomaticReferenceCounting.html)Soldierly
In my testing, it's only things like "zeroing weak references" that require a Deployment Target of iOS 5. You can still simply omit retains/releases and build for iOS 4.Eadwine
P
8

Q1: NO, 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:

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

Q2: NO, target can be as low as iOS 4.0

Prelect answered 2/5, 2012 at 7:50 Comment(0)
C
6

As far as I understand and as far as my iPhone/iPod running iOS 5 and iOS 4.3 respectively work, it's all quite automatic. An app I started for 4.0 and have "updated" to work with Xcode for iOS 5.0 never throws any sort of warning my way about releasing and retaining, even though it's all over every dealloc, etc. However, some of the same code I inserted (copied the file) into a new project created with Xcode for iOS 5 has many, many warnings. So it appears you don't have to remove all those calls, and no, it somehow adapts it automatically for older versions. Profiling my iPod, I see no memory leaks or other signs of failing deallocs/releasing. Does this help?

Cohn answered 16/6, 2011 at 7:47 Comment(1)
The latest beta version actually prompts me to remove all allocation calls, and that's about it. You can still retain/release but the system has the last word.Cohn
S
5

Regarding this part of your question

If I develop a new app for iOS 5 using ARC, will I need to implement some sort of "retro-compatibility" checks? I.e.: will I need to check the version of iOS and call retain and release accordingly? So, basically, is ARC available for all iOS versions or just for iOS 5?

It should be be noted that the iOS 5 compiler takes of the "retro-compatibility" (it actually ADDS the code to make retain/release work, essentially), but if you are not compiling for iOS 5.0, you cannot use weak as a keyword. Instead you use assign. This is unfortunate: weak is a huge advantage (no dangling pointers, ever!). See my question here for a discussion of weak, assign and ARC.

Soldierly answered 12/10, 2011 at 12:56 Comment(0)
L
2

If you can disable ARC by not using the new -fobjc-arc compiler flag, then you are not forced to re-write code moving forward - I guess (?)

Levorotatory answered 7/11, 2011 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.