• 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
Blocks: object retaining behavior?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Blocks: object retaining behavior?


  • Subject: Blocks: object retaining behavior?
  • From: Antonio Nunes <email@hidden>
  • Date: Wed, 14 Mar 2012 16:26:21 +0000

Hi,

I'm tracking down the cause of a crash, and am starting to wonder whether my understanding of what gets retained by a block is correct.

Lets say that we have the following method:

	[savePanel beginSheetModalForWindow:self.windowForSheet
					  completionHandler:^(NSInteger result) {
						  if (result == NSOKButton) {
							  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0L), ^{
								  [self myAsyncedHandler:savePanel.URL];
							  });
						  }
					  }];


My assumption is that the savePanel will be held on by the block for as long as it exists. But now I wonder whether that assumption is correct, and this retaining/copying behaviour only happens when the block is copied. So should we write something more like the following instead?

	[savePanel beginSheetModalForWindow:self.windowForSheet
					  completionHandler:^(NSInteger result) {
						  if (result == NSOKButton) {
							  NSURL *url = savePanel.URL.copy;
							  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0L), ^{
								  [self myAsyncedHandler:url];
								  [url release];
							  });
						  }
					  }];

In other words, is "savePanel.URL" guaranteed to exist for the lifetime of the block?

-António

----------------------------------------------------
There is nothing as strong as real gentleness, and
there is nothing as gentle as real strength.
----------------------------------------------------





_______________________________________________

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


  • Prev by Date: Re: [Q] Why is the threading and UI updating designed to be done only on a main thread?
  • Next by Date: Re: [Q] Why is the threading and UI updating designed to be done only on a main thread?
  • Previous by thread: Re: nil in cocoa collections
  • Next by thread: Re: Blocks: object retaining behavior?
  • Index(es):
    • Date
    • Thread