Tala
Tala

Reputation: 8938

What are the cases to use retain/release having ARC enabled

Having created a new iOS project I've enabled ARC and not planning to support iOS < 5.0. Does that mean I'll never need to use retain/release or there are cases that might need using them?

Can someone please list these cases if there are any, thanks!

Upvotes: 0

Views: 518

Answers (3)

nemesis
nemesis

Reputation: 1351

You will never need to use retain/release/autorelease. Having ARC enabled, frees you from writing memory management code, unless you're working with Core Foundation - ARC doesn't care about Core Foundation objects. But, you can let ARC release CF objects for you with a __bridge_transfer cast (or CFBridgingRelease). If you get it from a Cocoa or Cocoa Touch function or method, it's in Objective-C-land and therefore managed by ARC. You can transfer it to the CF world with a __bridge_retained cast (or CFBridgingRetain), after which you'll have to CFRelease it (or transfer it back to ARC). And yes, as long as the classes are compiled without ARC (which you can control on a file by file basis; go to Build Phases and add -fno-objc-arc as a flag to any file that should be compiled in an otherwise ARC'd project), then the compiled classes can override retain/release/autorelease to their heart's content.

Upvotes: 0

Geek
Geek

Reputation: 8320

No, you don't need to use them as ARC is enabled.

Retain/release are methods used retain and release a reference to an object, respectively. It is used to manage memory allocation and deallocation. User has to manage memory by himself, only when ARC is not enabled or not available as in below iOS 4.3.

ARC is Automatic Reference Counting. When enabled, SDK itself decides when to release an object. User just need to allocate it. User can still manage allocation of no. of objects by declaring either a strong or weak reference to an object.

Upvotes: 0

Rad&#39;Val
Rad&#39;Val

Reputation: 9251

You should read Apple's migration guide. There are a few caveats. However, there is no situation in which you have to use retain/release yourself, except if you work with Core Foundation directly or if you specifically mark individual files with -fno-objc-arc and take ownership in releasing the memory yourself for that file only.

Upvotes: 4

Related Questions