On 2012-01-26, at 3:51 PM, Jan E. Schotsman wrote:
> Hello,
>
> This code is given in the "Transitioning to ARC Release Notes" as an example
> of accomodating blocks in an ARC environment:
>
> __block MyViewController *myController = [[MyViewController alloc] init…];
> // ...
> myController.completionHandler = ^(NSInteger result) {
> [myController dismissViewControllerAnimated:YES completion:nil];
> myController = nil;
> };
>
> Supposedly this avoids a retain cycle. But where is the cycle? At least two
> objects are needed for a cycle. What is the second one?
ARC has changed the behaviour of __block [1] so that it forces a retain of the
value (previously, it forced a variable to behave like a weak property). This
(as I understand it) is necessary because otherwise the value would be released
at the end of the current method, which is obviously not what you want. Note
that, even if you don't specify myController as __block, the variable will
still be declared with an implicit __strong attribute.
The key is that, by declaring it as __block, you give your block the option to
alter its contents. At the end of the block, you simply force it to Nil, which
implicitly releases it, thus breaking the retain cycle. Pretty neat, eh?
HTH,
—Mt.
[1]: See the CLANG docs, section 7.5.
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]