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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to