• 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: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"


  • Subject: Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
  • From: email@hidden
  • Date: Mon, 09 Jul 2012 23:17:43 -0700

>
> On 09.07.2012, at 18:03, Fritz Anderson wrote:
>
>> You can break this by having a strong reference to self that the block
>> can manage independently.
>>
>>   __block MyClass *      blockSelf = self;
>>   [self.operationQueue addOperationWithBlock:^{
>>       [blockSelf bar];
>>       blockSelf = nil;
>>   }];
>
> Thank you Fritz for your answer.
>
> But if I break the cycle and if I understand it correctly, 'self' won't be
> retained anymore within the block. If there are no more references to
> 'self' (except one possibly within the block), I fear 'self' will be
> destroyed before the block executes. I do require, though, that 'self'
> will be valid as long as the block is not finished.
>
>
>
> Note: I'm using ARC in which case a retainable pointer declared with
> __block will be retained (as opposed to manual ref counting), so I should
> use __weak or __unsafe_unretained instead of __block to break the cycle
> (if that is what I have to do).
>
> Andreas

No, if you use the syntax Fritz suggests and there is no need to fear self
being destroyed before the block is finished. This is also in the
Transitioning To ARC release notes.

blockSelf is retained by the block, since blockSelf just points to self,
that retains self. At the end of the block, but not before, you're setting
it to nil, which gets rid of that strong reference. All this little trick
really does is 'rename' self to something else so you can manage its
lifecycle.


_______________________________________________

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: 
 > "Capturing 'self' strongly in this block is likely to lead to a retain cycle" (From: Andreas Grosam <email@hidden>)
 >Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle" (From: Fritz Anderson <email@hidden>)
 >Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle" (From: Andreas Grosam <email@hidden>)

  • Prev by Date: Re: turning app into background app
  • Next by Date: Re: One more try - NSCollectionView multi-selection problem..
  • Previous by thread: Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
  • Next by thread: Open Recent Menu
  • Index(es):
    • Date
    • Thread