How does the __block specifier work?

cocoa-touch, objective-c, objective-c-blocks

Solution

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.

Problem

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)

Original source