Re: ARC and blocks
Re: ARC and blocks
- Subject: Re: ARC and blocks
- From: Jeff Kelley <email@hidden>
- Date: Thu, 26 Jan 2012 19:09:31 -0500
Without ARC, you would use __block to prevent the block from retaining the object and causing the retain cycle. With ARC, however, the object is retained when you put it into the variable, so to avoid a retain cycle, you have to declare it like so:
__unsafe_unretained __block MyViewController *myController = …
It looks awful, yes, but without the first piece we were having extreme memory leaks.
Jeff Kelley
On Jan 26, 2012, at 5:34 PM, David Duncan wrote:
> On Jan 26, 2012, at 1: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?
>> Excuse me if I'm being dumb.
>
>
> myController retains/copies the block. Then the block retains myController (under ARC __block variables are strong references, so they get retained). Thus you are left with myController retains the block which retains myController.
> --
> David Duncan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden