Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>// This WILL NOT increment the image's retain count. CGImageRef iRef = (__bridge CGImageRef)array[0]; // This WILL increment the image's retain count. CGImageRef iRef = (__bridge_retained CGImageRef)array[0]; </code></pre> <p>Since <code>__bridge_retained</code> increments the retain count, you need to decrement the retain count at some point later. Because <code>__bridge_retained</code> acts like <code>CFRetain</code>, Apple created a <code>__bridge_retained</code> wrapper called <code>CFBridgingRetain</code> that reminds you of this:</p> <pre><code>// This WILL increment the image's retain count. CGImageRef iRef = CFBridgingRetain(array[0]); </code></pre> <p>Whenever you see <code>CFRetain</code> <strong>or</strong> <code>CFBridgingRetain</code>, you know you need to release the object at some point later.</p> <p>Similarly, you can use <code>CFBridgingRelease</code> instead of <code>__bridge_transfer</code> to decrement the retain count of a CF object. Example:</p> <pre><code>[_array addObject:CFBridgingRelease(renderedRef)]; </code></pre> <p>You can use <code>CFBridgingRelease</code> to balance a <code>CFRetain</code> or a <code>CFBridgingRetain</code>. It returns an <code>id</code> that ARC manages.</p> <p>A normal <code>NSMutableArray</code> retains each of its elements. You can tell it to become empty:</p> <pre><code>[_array removeAllObjects]; </code></pre> <p>When you do this, it will release each of its elements, balancing the retain it performed on each element.</p> <p>Thus there is no memory leak in this code:</p> <pre><code>CGImageRef image = CGImageCreate(...); [_array addObject:CFBridgingRelease(image)]; [_array removeAllObjects]; </code></pre> <p>or in this code:</p> <pre><code>CGImageRef image = CGImageCreate(...); [_array addObject:CFBridgingRelease(image)]; CGImageRef image2 = CFBridgingRetain([_array lastObject]); [_array removeLastObject]; CGContextDrawImage(gc, rect, image2); CGImageRelease(image2); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload