• 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
capturing self is likely to lead to a retain cycle ..
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

capturing self is likely to lead to a retain cycle ..


  • Subject: capturing self is likely to lead to a retain cycle ..
  • From: Roland King <email@hidden>
  • Date: Sat, 12 Nov 2011 16:14:31 +0800

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

  • Follow-Ups:
    • Re: capturing self is likely to lead to a retain cycle ..
      • From: Jeff Kelley <email@hidden>
  • Prev by Date: Re: 2 itunesconnect questions
  • Next by Date: Re: How to determine if there is an internet connection available?
  • Previous by thread: Re: How to determine if there is an internet connection available?
  • Next by thread: Re: capturing self is likely to lead to a retain cycle ..
  • Index(es):
    • Date
    • Thread