• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ARC and blocks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >ARC and blocks (From: "Jan E. Schotsman" <email@hidden>)
 >Re: ARC and blocks (From: Conrad Shultz <email@hidden>)

  • Prev by Date: Re: ARC and blocks
  • Next by Date: Binding Leak - Turn off Raises For Not Applicable Keys
  • Previous by thread: Re: ARC and blocks
  • Next by thread: Re: ARC and blocks
  • Index(es):
    • Date
    • Thread