Re: ARC and blocks
Re: ARC and blocks
- Subject: Re: ARC and blocks
- From: Roland King <email@hidden>
- Date: Fri, 27 Jan 2012 08:34:05 +0800
On Jan 27, 2012, at 6:44 AM, Conrad Shultz wrote:
> On 1/26/12 1:51 PM, Jan E. Schotsman wrote:
>>
>>
>
> The block normally would retain variables it captures from its scope,
> including, in this case, myController. Presumably myController would
> retain the completionHandler block, ergo a retain cycle.
>
> However, __block variables are NOT retained automatically by a block
> during capture, so this breaks the retain cycle.
under ARC, __block variables ARE retained automatically, if they weren't, there wouldn't be a cycle.
>
> So, when myController is nil'ed out, ARC releases it, and it releases
> the block in turn. No leaks/abandoned memory.
That is correct, and the variable has to be a __block variable to allow you to modify it.
>
> A special form of this is the idiom:
>
> __block id mySelf = self;
>
> ^{
> [mySelf doSomething];
> }
>
> This lets you use self in a block without retaining it.
Not so under ARC, mySelf retains self, but it's ok, the block releases it at the end so it all balances out.
_______________________________________________
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