How should I manage memory for C block feature? (Apple extension)
Apple introduced a closure in C as name of 'block'. Should I manage memory for the blocks? If so, wha开发者_运维问答t do I have to do?
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.
精彩评论