Blocks: object retaining behavior?
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