Re: capturing self is likely to lead to a retain cycle ..
Re: capturing self is likely to lead to a retain cycle ..
- Subject: Re: capturing self is likely to lead to a retain cycle ..
- From: Jeff Kelley <email@hidden>
- Date: Sat, 12 Nov 2011 07:56:53 -0500
ARC doesn’t know in the context of that method what will happen to the
block. Each method lives in a vacuum with retain counts being consistent at
the beginning and end. So, even though you know that the block will be
released, that’s not clear in the context of the method. I would recommend
the approach of a weak reference to self.
Jeff Kelley
On Sat, Nov 12, 2011 at 3:14 AM, Roland King <email@hidden> wrote:
> I have a UIViewController which is presented modally, it has a textfield
> which allows editing and a model object, call it foo, on which it can
> attempt to set the 'topicName' property. Because I'm dealing with
> UIDocument(s) and trying to be nicely iCloud compatible and the topic name
> change does require a background call, the setTopicName: method cannot
> directly return YES or NO, it has a completionHandler which runs after the
> operation is attempted (and on the main thread). I'm using that completion
> handler to either pop up an alert sheet saying it failed, or call the
> onCancel: method of myself to dismiss the modal window.
>
> With the code below I'm getting a warning "Automatic Reference Counting
> Issue: Capturing 'self' strongly in this block is likely to lead to a
> retain cycle", but I don't see why. I understand that 'self' is retained by
> the block, but after the block runs, it should be released again. I've
> re-read the ARC guide and understand why, if you set a block as a property
> of yourself and that block uses self, it can cause a retain cycle, but I'm
> not setting this block as a property and I don't see how, for an anonymous
> block which is passed down the call chain and run, there would be a retain
> cycle.
>
> Am I missing the blindingly obvious, or is ARC just being very cautious
> about ANY block retaining self and suggesting I use another pattern? I can,
> I can easily set up a weak reference to myself just before the block and
> use it .. I just want to understand why.
>
>
> -(BOOL)textFieldShouldReturn:(UITextField*)textField
> {
> [ foo setTopicName:[ textField text ]
> completionHandler:^(BOOL)success{
>
> if( !success )
> {
> // present an alert sheet
> }
> else
> {
> // onCancel is also the callback from the 'cancel'
> button and dismissed this modally presented viewcontroller
> [ self onCancel:nil ]; // <-- ARC not too happy
> with this
> }
> } ];
> }_______________________________________________
>
> 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
>
_______________________________________________
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