Reputation: 21553
I can specify an a variable on stack with a __block
specifier and then I am able to modify it in a block. I am just wondering, behind the scenes what happens? (If the block is executed sometime in the future, then the stack might be cleared)
Upvotes: 3
Views: 1341
Reputation: 90541
When a block which references a __block
variable is copied, the variable is moved to the heap. This means that all code which references it has to do so through an indirect means, basically a pointer, so that, when it moves from the stack to the heap, those references can switch along with it.
This is documented here.
Upvotes: 4