7

I have developed an iPhone application which should support iOS4 and iOS5 based iPhone/iPad.

my application leaks memory at few places which is becoming hard to debug due to the size of code. I recently read about ARC (Automatic Reference Counting), my query is

  1. do i need to modify my source code (retain/release /alloc/dealloc) to compile with ARC. also what all changes we need to perform using ARC?

  2. is it advisable to shift to ARC?

  3. will my app work on iOS4 phone if i use ARC

thanks.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Rohit
  • 6,941
  • 17
  • 58
  • 102
  • I suggest making this question a wiki entry... it's definitely a good question and definitely programming related, but most of the question is really opinion and related to specific circumstances. – Jason Coco Jan 05 '12 at 20:35
  • 1
    You're asking three different questions here. The second is covered by [iOS 5 Best Practice (Release/retain?)](http://stackoverflow.com/questions/6308425/ios-5-best-practice-release-retain), and the third by [if convert project to Automatic Reference Counting(ARC), Is it still support on iOS 3.X, 4.X?](http://stackoverflow.com/questions/6421753/if-convert-project-to-automatic-reference-countingarc-is-it-still-support-on) – Brad Larson Jan 05 '12 at 23:57

3 Answers3

14

This is probably not the best place to have posted this question, but I will answer because I don't mind the question being here.

http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html

  1. you will use the migration tool 'Edit > Refactor > Convert to Objective-C ARC' and hand fix anything the tool can't figure out.

  2. Yes.

  3. Yes but Zeroing weak references will not.

griotspeak
  • 13,022
  • 13
  • 43
  • 54
  • One other consideration is that many third party libraries do not have good arc versions yet. This is keeping me from upgrading some of my existing projects. – Peter DeWeese Jan 05 '12 at 20:46
  • 2
    You can selectively exclude any source file from ARC when you run the conversion. Just un-tick any third party class files in the ARC conversion tool preflight check stage and it will skip them and mark them with -fno-objc-arc so they are excluded from ARC validation. This is one of the best features of ARC - that you can mix and match ARC and non-ARC files in the same project. – Nick Lockwood Jan 05 '12 at 23:49
4

I definitely think that it's good for programmers to understand memory management and how the system actually works... but, I think ARC is a very good system and works really well. This is really an opinion question, so my opinion is that it's almost always worth starting new projects that are going to target iOS 5 apps on ARC, except in very specific circumstances.

I feel that if you're using a lot of C libraries in your code, ARC is a little bit harder to use right now (so if you're mostly using third-party C libraries and things like CoreFoundation, you might consider whether it makes sense or not), but even then, if these libraries are mostly isolated from your Objective-C controllers and such, ARC is still good.

For older apps, you need to look at your app usage and patterns. If you use a lot of delegate methods, since you can't use weak references on iOS 4, it gets a little more tricky and you'll probably have to have mixed ARC and non-ARC code. It might be better to make a design decision to move forward with ARC. So new features are designed for iOS 5 and maybe not available (or fully available) in the iOS 4 version of the app, and those use ARC.

Really, in the end, it will depend on how your application is already designed, how large it is, and how comfortable you are with managed memory management and the usage/restrictions of ARC. For example, I have three projects that I'd never convert to ARC, one that I'm doing mixed right now, one that's fully converted (but still targets iOS 4+) and 2 that are full-on ARC and iOS 5+ only.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • OpenGL is technically a C API, albeit without any sort of direct object retention therefore no need for 'bridges'; All objects are internal and opaque and accessed through functions and integer IDs. Still, is there anything to be aware of when using OpenGL within ARC Obj-C code? – Nicolas Miari Jun 19 '12 at 20:15
1

To be clear, whilst you can't use ARC weak references if you are targeting iOS 4, you can still use unsafe_unretained, which is basically the equivalent of using assign for object properties. That means that you can convert any well-written non-ARC code to ARC without accidental creation of retain cycles on iOS 4.

By using unsafe_unretained you lose out on the auto-null feature of weak references, but you still get all the other benefits of ARC such as not having to worry about forgetting to release ivars in your dealloc statements, etc.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103