Re: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
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: Andreas Grosam <email@hidden>
- Date: Tue, 10 Jul 2012 07:39:02 +0200
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
_______________________________________________
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