Apple introduced a closure in C as name of 'block'. Should I manage memory for the blocks? If so, what do I have to do?
+1
A:
Like variables with function scope in plain C, blocks literals in functions have automatic storage duration. When you think of a block literal as a pointer type you see what can go wrong: When you return a block from a function you are returning a pointer to local memory, which is no longer valid.
The solution is to use the Block_copy()
function whenever you want to return a block and the Block_release()
when you no longer need it.
More information in this tutorial that has a separate section on blocks in C.
schot
2010-08-17 07:46:41