• 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
Re: Anything like -[performBlockOnMainThread:]?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Anything like -[performBlockOnMainThread:]? (From: Rick Mann <email@hidden>)
 >Re: Anything like -[performBlockOnMainThread:]? (From: Clark Cox <email@hidden>)
 >Re: Anything like -[performBlockOnMainThread:]? (From: Rick Mann <email@hidden>)
 >Re: Anything like -[performBlockOnMainThread:]? (From: Clark Cox <email@hidden>)
 >Re: Anything like -[performBlockOnMainThread:]? (From: Rick Mann <email@hidden>)
 >Re: Anything like -[performBlockOnMainThread:]? (From: Clark Cox <email@hidden>)

  • Prev by Date: Re: NSString after an UIAlertView..
  • Next by Date: Re: User-space threads and NSAutoreleasePool
  • Previous by thread: Re: Anything like -[performBlockOnMainThread:]?
  • Next by thread: Re: Anything like -[performBlockOnMainThread:]?
  • Index(es):
    • Date
    • Thread