Re: __block __weak - am I doing this right?
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