title: Which __bridge toll free bridging should I use for casting an Objective-C convenience constructor into a C ref?
body: <p>I'm trying to convert manual retain release code to ARC. </p>
  
  <p>I'm struggling to figure out the correct way to toll free bridge when I have an Objective-C convenience constructor whose return pointer value is being stored in a CFTypeRef. </p>
  
  <p>Existing code, using MRR:</p>
  
  <pre><code>@interface SourceItemCell UITableViewCell
  {
    CATextLayer *mSourceText;
  }
  
  @implementation SourceItemCell
  - (id)init
  {
    self = [super init];
    mSourceText = [CATextLayer layer];
  
    // key line I'm wondering about:
    mSourceText.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
  
    [[self contentView].layer addSublayer:mSourceText];
    return self;
  }
  </code></pre>
  
  <p>To keep you from having to look up the documentation, the font property of CATextLayer is of type CFTypeRef. </p>
  
  <p>It seems like my options are:</p>
  
  <pre><code>mSourceText.font = (__bridge CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>mSourceText.font = (__bridge_transfer CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>mSourceText.font = (__bridge_retained CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
  </code></pre>
  
  <p>Here's my thinking. The clearest guide to toll free bridging I've found is <a href="http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html" rel="nofollow noreferrer">http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html</a> . There's a similar example of a cast from an Objective-C type to a C type, about which he writes:</p>
  
  <blockquote>
    <p>By using __bridge_retained, we can tell ARC to transfer ownership out of the system and into our hands. Since ownership is transferred, we're now responsible for releasing the object when done with it, just like with any other CF code</p>
  </blockquote>
  
  <p>...otherwise, if we were only to use __bridge, ARC would not make any effort to hold onto the memory on our CFTypeRef's account. </p>
  
  <p>So here is what I think is the most sensible way to go:</p>
  
  <pre><code>mSourceText.font = (__bridge_retained CFTypeRef)[UIFont fontWithName:@"HelveticaNeue" size:12.0];
  ... // At some later point
  CFRelease(mSourceText.font);
  </code></pre>
  
  <p>Now, if that's correct, I'm still pretty unclear on when I could be sure it's safe to release, but if I never released, it would at least be just a small memory leak, right? </p>
  
  <p>In conclusion, my actual questions are:</p>
  
  <ol>
  <li>Is my proposed code correct?</li>
  <li>Where should I CFRelease this object? In the dealloc function for SourceItemCell?</li>
  </ol>
  
  <p>Here's why I didn't think related questions answered my question:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/8842953/must-i-use-bridge-or-bridge-retained-if-im-bridging-an-autoreleased-object">Must I use __bridge or __bridge_retained if I&#39;m bridging an autoreleased object to Core Foundation?</a> I'm not sure if it matters that the Objective-C convenience constructor is explicitly saved to an Objective-C variable on the previous line. Also, the approved answer says to only use __bridge_retained "if you want to manage the lifecycle" of the C object, which I think is wrong... I get the sense lots of people are using __bridge_retained because they <em>have</em> to manage the lifecycle themselves. </li>
  <li><a href="https://stackoverflow.com/questions/14854521/where-and-how-to-bridge">Where and how to __bridge</a> Approved answer has helpful summary, but focuses on non-retained example. </li>
  </ol>
  
  <p>PS. Please don't judge me because I'm using Helvetica... :) </p>
  
  <p>Edit:</p>
  
  <p>When I use __bridge_retained, and do static analyzer, I get this complaint:</p>
  
  <p><img src="https://i.stack.imgur.com/hA9vP.png" alt="Static analyzer complaint"></p>
  
  <p>"Property returns a Core Foundation object with a +0 retain count.
  Incorrect decrement of the reference count of an object that is not owned at this point by the caller."</p>
  
  <p>(The mDelegate and IS_ARC lines are, I believe, irrelevant to this issue.)</p>
  
  <p>So there's something I'm fundamentally not understanding correctly... </p>