• 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: __block __weak - am I doing this right?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: __block __weak - am I doing this right?


  • Subject: Re: __block __weak - am I doing this right?
  • From: Wim Lewis <email@hidden>
  • Date: Wed, 15 Feb 2012 16:32:31 -0800

On 15 Feb 2012, at 12:58 PM, Matt Neuburg wrote:
> * Why was I crashing until I said __block?


Without __block, the value of the variable is copied into the block (effectively as a const local variable) when the block is created. With __block, both the code inside and outside the block reference the same variable, and each can see each others' modifications (almost like a closure in languages that have that).

The block is created before 'observer' is initialized (it has to be, so that it can be passed to -addOberver...), so the value of 'observer' it captures when it is created is the value of the uninitialized stack variable. It's as if you were doing this:

  id observer = createObserverStuff(observer);

instead of this, which is kinda sorta what happens with __block variables:

  id observer = createObserverStuff(&observer);



_______________________________________________

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: 
 >__block __weak - am I doing this right? (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: __block __weak - am I doing this right?
  • Next by Date: Re: [ Was NSReadPIxel ]
  • Previous by thread: Re: __block __weak - am I doing this right?
  • Next by thread: Re: __block __weak - am I doing this right?
  • Index(es):
    • Date
    • Thread