Instead of accessing <code>self</code> directly, you should access it indirectly, from a reference that will not be retained. If you're not using Automatic Reference Counting (ARC), you can do this:
If you are using ARC, the semantics of <code>__block</code> changes and the reference will be retained, in which case you should declare it <code>__weak</code> instead.
Let's say you had code like this:
The problem here is that self is retaining a reference to the block; meanwhile the block must retain a reference to self in order to fetch its delegate property and send the delegate a method. If everything else in your app releases its reference to this object, its retain count won't be zero (because the block is pointing to it) and the block isn't doing anything wrong (because the object is pointing to it) and so the pair of objects will leak into the heap, occupying memory but forever unreachable without a debugger. Tragic, really.
That case could be easily fixed by doing this instead:
In this code, self is retaining the block, the block is retaining the delegate, and there are no cycles (visible from here; the delegate may retain our object but that's out of our hands right now). This code won't risk a leak in the same way, because the value of the delegate property is captured when the block is created, instead of looked up when it executes. A side effect is that, if you change the delegate after this block is created, the block will still send update messages to the old delegate. Whether that is likely to happen or not depends on your application.
Even if you were cool with that behavior, you still can't use that trick in your case:
Here you are passing <code>self</code> directly to the delegate in the method call, so you have to get it in there somewhere. If you have control over the definition of the block type, the best thing would be to pass the delegate into the block as a parameter:
This solution avoids the retain cycle and always calls the current delegate.
If you can't change the block, you could deal with it. The reason a retain cycle is a warning, not an error, is that they don't necessarily spell doom for your application. If <code>MyDataProcessor</code> is able to release the blocks when the operation is complete, before its parent would try to release it, the cycle will be broken and everything will be cleaned up properly. If you could be sure of this, then the right thing to do would be to use a <code>#pragma</code> to suppress the warnings for that block of code. (Or use a per-file compiler flag. But don't disable the warning for the whole project.)
You could also look into using a similar trick above, declaring a reference weak or unretained and using that in the block. For example:
All three of the above will give you a reference without retaining the result, though they all behave a little bit differently: <code>__weak</code> will try to zero the reference when the object is released;<code>__unsafe_unretained</code> will leave you with an invalid pointer; <code>__block</code> will actually add another level of indirection and allow you to change the value of the reference from within the block (irrelevant in this case, since <code>dp</code> isn't used anywhere else).
What's best will depend on what code you are able to change and what you cannot. But hopefully this has given you some ideas on how to proceed.
<a href="http://stackoverflow.com/posts/7854315/revisions">edited Nov 2 '11 at 1:38</a>
answered Oct 21 '11 at 19:25
<a href="http://stackoverflow.com/users/10947/benzado">benzado</a>
32.1k972100
1
7
<a href="http://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api">add / show 5 more comments</a>

<a></a>
There’s also the option to suppress the warning when you are positive that the cycle will get broken in the future:
That way you don’t have to monkey around with <code>__weak</code>, <code>self</code> aliasing and explicit ivar prefixing.
answered Feb 14 '12 at 7:51
<a href="http://stackoverflow.com/users/17279/zoul">zoul</a>
46.8k14112201
<a href="http://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api">add comment</a>
I believe the solution without ARC also works with ARC, using the <code>__block</code> keyword:
<a href="http://stackoverflow.com/posts/7854645/revisions">edited Oct 24 '11 at 15:22</a>
answered Oct 21 '11 at 19:59
<a href="http://stackoverflow.com/users/34101/tony">Tony</a>
1,988711
For a common solution, I have these define in the precompile header. Avoids capturing and still enables compiler help by avoiding to use <code>id</code>
Then in code you can do:
歡迎加群互相學習,共同進步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,轉載請注明出處!
本文轉自張昺華-sky部落格園部落格,原文連結:http://www.cnblogs.com/sunshine-anycall/p/3514235.html,如需轉載請自行聯系原作者