Re: Anything like -[performBlockOnMainThread:]?
Re: Anything like -[performBlockOnMainThread:]?
- Subject: Re: Anything like -[performBlockOnMainThread:]?
- From: Benjamin Rister <email@hidden>
- Date: Fri, 19 Mar 2010 13:13:19 -0400
> dispatch_sync(dispatch_get_main_queue(), ^{
> ...
> });
An important reminder to anybody using this technique: be careful doing dispatch_sync to the main queue under GC. There’s a bug (rdar://problem/7455071) that can cause objects to not be kept alive by the block across that thread switch, leading to crashes and other bad things.
This is a workaround you can use instead of dispatch_sync:
void dsfixed_dispatch_sync(dispatch_queue_t queue, dispatch_block_t block) {
dispatch_block_t copiedBlock = Block_copy(block);
dispatch_sync(queue, copiedBlock);
Block_release(copiedBlock);
}
Since the problem only occurs with dispatching to the main queue, it might be better to specialize it for that purpose, but we started using this before the details on the bug were nailed down, so our workaround is still generic. Note also if you’re rolling your own that -copy is not a workable substitute for Block_copy in this case; you can still be nailed by the bug.
Best,
Benjamin Rister_______________________________________________
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