With ARC, many people didn’t understand how to use retain/release/autorelease efficiently can be relieved from such a headache. Well, Garbage Collection was an answer, but GC was slow and have shown some problems.
If writing in Objective-C/AppKit/non-Core * always, there is nothing worry about memory management usually except for the case of blocks. However, nowadays many of Core * data types and structures don’t have their counter part in AppKit layer. ( After iOS became very popular, Apple people started to fail in managing the layers of Obj-C OOP frameworks and underlying Core * frameworks. )
If you use Core * objects and types, you will use __bridge, __bridge_retained and __bridge_transfer keywords very often. However, problem is that it’s pretty difficult to remember which one to use when. So, let’s think about how to memorize them.
One thing to keep in mind is that ARC is largely designed and implemented for OOP framework. So, the inner circle of ARC is Objective-C interface ( class ) objects.
Ok, now here it goes.
- __bridge : the simplest form. Doesn’t transfer ownershipbetween Obj-C and Core * types
- Use this when the caller ( when you pass a pointer of an object through message parameter ) is expected to manage it.
- Use this when you assign a pointer to an object to a member variable ( or property ) of another object, but the caller side still wants to manage it.
- The receiver is expected to have __weak reference on the passed object.
- __bridge_retained : Transfer ownership from Obj-C to Core * / C types
- Core Foundation, for example, side needs to manage it. So, the Core Foundation side needs to “retain” it.
- CFBridgeRetain( Obj-C object ) can be used alternatively
- If it is non-ARC environment, it will be like that you add an NSNumber object to CFArray and the CFArray instance manages it. So, you can call “release” to the instance of NSNumber after adding to the instance of CFArray. The CFArray will “retain” it when added. So, you can safely call “release” for the NSNumber instance after adding to the CFArray instance.
- Obj-C => Core *
- __bridge_transfer : Transfer ownership from Core */C to Obj-C types
- Objective-C side needs to manage Core Foundation, for example, objects or data types.
- CFBridgeRelease( Core Foundation object ) is an alternative.
- Why isn’t it CFBridgeTransfer()? or Why isn’t it __bridge_release? Yeah. Here I think Apple people lost consistency.
- As I mentioned earlier, ARC is created for Obj-C object management in mind. So, the compiler part of Objective-C knows how to manage those. So, it will be easier if you memorize like “Ok. I will just transfer ownership to Obj-C. Then it will manage in a way it needs to. So, I don’t need to specify whether to retain it or not.”
- On the other hands, from Core * point of view, it needs to hand off from managing it. So, CFBridgeRelease().
- Obj-C <= Core *
So, it’s my way to memorize those __birdge* keywords. However, eventually I believe Apple people should make consistent between __bridge* and CFBridge*() and also their names should convey what they do more clearly.