I am trying to convert a manual save release code to ARC.
I am trying to figure out the correct path to a free bridge when I have an Objective-C convenience constructor whose return value is stored in CFTypeRef.
Existing code using MRR:
@interface SourceItemCell UITableViewCell { CATextLayer *mSourceText; } @implementation SourceItemCell - (id)init { self = [super init]; mSourceText = [CATextLayer layer];
So you donβt have to look for documentation, the CATextLayer font property is of type CFTypeRef.
It looks like my options are:
mSourceText.font = (__bridge CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
or
mSourceText.font = (__bridge_transfer CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
or
mSourceText.font = (__bridge_retained CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
Here is my thinking. The vivid guide to the free bridging I found is http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html . There is a similar example of casting an Objective-C type to a C type, which he writes about:
Using __bridge_retained, we can inform ARC of the transfer of ownership from the system to our hands. Since ownership is transferred, we are now responsible for the release of the property when it is completed with it, as with any other CF code.
... Otherwise, if we used only __bridge, ARC would not have made any effort to keep the memory in our CFTypeRef account.
So here is what I consider the most sensible way:
mSourceText.font = (__bridge_retained CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0]; ...
Now, if this is correct, I still do not quite understand when I can be sure that it is safe to free it, but if I never release it, it will be at least just a small memory leak, right?
In conclusion, my current questions are:
- Is my suggested code correct?
- Where can I get CFRelease this object? In dealloc function for SourceItemCell?
This is why I did not think related questions answered my question:
PS. Please do not judge me, because I use Helvetica ... :)
Edit:
When I use __bridge_retained and do a static analyzer, I get this complaint:

"The property returns a Core Foundation object with a +0 save count. Incorrect decrement of the reference counter of an object that is not currently owned by the caller.
(The mDelegate and IS_ARC lines, I believe, are not relevant to this problem.)
So, I fundamentally donβt understand something correctly ...