How should I manage memory for C block feature? (Apple extension)
-
29-09-2019 - |
Pergunta
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?
Solução
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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow