Reputation: 2170
This is to clear up some doubt in my conceptual understanding of ARC . If ARC is a compile time technology why isnt it available on all versions of iOS and OS X ?
Upvotes: 3
Views: 1381
Reputation: 185841
ARC isn't just a compile-time technology. It also relies on some runtime components. There's two parts to this:
The reference counting. At compile-time, ARC introduces calls to a bunch of helper functions. These are all documented online, but the important ones are objc_retain()
, objc_release()
, and objc_autorelease()
. These do the same things as calling -retain
, -release
, or -autorelease
on the object, but there's some extra optimizations that can be done under ARC with the runtime functions (notably, when a method returns an autoreleased object and the caller stores the value into a strong variable, the autorelease + retain may be able to be skipped entirely). These runtime functions are what adds the OS requirement. Luckily, if you target an older OS, Xcode can link in a static library called libarclite
that provides implementations of these functions, thus allowing you to use ARC when targeting an older OS (e.g. iOS 4.3). The downside is these functions don't have the optimizations that "true" ARC does, so performance may suffer slightly.
Zeroing weak reference support. This also requires a set of runtime functions, plus some changes to the internals of how memory management works in the runtime, plus some changes in the frameworks to better support zeroing weak references. The memory management changes specifically are why zeroing weak reference support cannot be backported using libarclite
like the rest of ARC can.
Upvotes: 6
Reputation: 386038
ARC requires some runtime functions to be present. These functions are in the clang ARC documentation.
iOS 5.0 and Mac OS X 10.7 (and later versions) include these functions.
To allow you to deploy apps to older OS versions, Apple includes a static library called “arclite” which defines most of those functions. If your deployment target is an older OS, Xcode links your app with arclite.
So you might ask, why doesn't arclite work on all old versions of iOS and OS X? I don't know for sure (and maybe nobody outside of Apple does). One reasonable guess is that arclite needs to hook into the Objective-C runtime. The runtime can change between versions of the OS. Apple didn't want to try to support every version of the runtime that they've ever shipped, so they picked reasonable cutoffs and made arclite support every version since those cutoffs. This simplifies the arclite implementation and reduces the testing burden.
Note that arclite doesn't include support for zeroing weak references. Perhaps this is a feature that was too hard to support on older versions of the runtime, even those after the cutoff.
Upvotes: 2