Tuesday, January 22, 2013

Blocks, Operations, and Retain Cycles

Conrad Stoll (via Jonathan Penn):

What we had to do is just make the operation referenced by the block into a weak reference. That way, the block isn’t holding onto the operation, it just has a weak reference to it. It’s worth pointing out that pre-ARC, __block was sufficient to prevent a retain cycle. But with ARC, __block no longer carries the same meaning and __weak must be used to prevent a retain cycle. When the operation is finished and gets released by the operation queue, it will go away. That will then release the block, which releases the image and graphics contexts just like we see it’s supposed to in the sample above. That resulted in dramatically improved memory performance and completely normal memory behavior. Literally just that 2 line fix there.

It seems like NSBlockOperation shouldn’t provide access to its -executionBlocks; then it could release them as they complete or when it’s canceled. (Alas, the API uses “cancelled” while nearly everywhere else Apple uses the single-l form.)

Comments RSS · Twitter

Leave a Comment