Recursion and blocks are complex. Since the block captures all the passed variables, the _test_closure variable is not yet initialized (and clang should give you a warning:
Block pointer variable '_test_closure' is not initialized when captured by the block
)
There are several ways to get around this, but the most obvious and easiest is to simply make the __block variable block itself (as @ H2CO3 said). This allows the block to be weak-linked almost, so when you call it again, it initializes correctly.
Another option you have is making the block global or static, for example:
// outside of 'main', thus being a global variable void (^blockRecurse)(int) = ^(int level) { if (level < 0) return; NSLog(@"Level: %i", level); blockRecurse(--level); }; int main() { @autoreleasepool { blockRecurse(10); } }
This means that it is not captured by the block, but instead refers to a global / static variable that can be changed equally by the whole code.
Richard J. Ross III
source share