简体   繁体   English

我应该如何管理C块功能的内存? (Apple扩展名)

[英]How should I manage memory for C block feature? (Apple extension)

Apple introduced a closure in C as name of 'block'. 苹果在C语言中引入了一个闭包,称为“ block”。 Should I manage memory for the blocks? 我应该管理块的内存吗? If so, what do I have to do? 如果是这样,我该怎么办?

Like variables with function scope in plain C, blocks literals in functions have automatic storage duration. 像普通C语言中具有函数作用域的变量一样,函数中的块字面量具有自动存储时间。 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. 解决方案是,每当您要返回一个块时都使用Block_copy()函数,而在不再需要它时则使用Block_release()

More information in this tutorial that has a separate section on blocks in C. 本教程中的更多信息单独介绍了C语言中的块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM