CFSTR内存管理
我正在使用 CFSTR 函数从常量 c 字符串创建 CFString,并且我在守护进程中非常频繁地调用此函数。
来自文档:
CFSTR 返回的值具有以下语义:
- 从 CFSTR 返回的值不是由 CFString 释放的 - 它们是 保证有效直到程序终止。
- 你可以保留 并以平衡的方式释放从 CFSTR 返回的值,例如 任何其他 CFString,但您不需要这样做。
我应该使用保留和释放吗?
I am using CFSTR
function for creating CFString from constant c string and i am calling this function very frequently in my Daemon.
From documentation:
A value returned by CFSTR has the following semantics:
- Values returned from CFSTR are not released by CFString—they are
guaranteed to be valid until the program terminates. - You can retain
and release values returned from CFSTR in a balanced fashion, like
any other CFString, but you are not required to do so.
Should i use retain and release ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如文档所述,
CFSTR()
创建的字符串在程序终止之前保持有效。您可以整天释放它们,但它们实际上不会被释放。因此,无需显式保留/释放它们。保留/释放它们是有效的,因为否则,您无法通过保留/释放它们的其他代码(框架方法等)传递它们。像对待使用@""
创建的 NSString 文字一样对待它们,也就是说,创建后无需保留或释放它们,但如果您正在编写可以接受任何 CFString 的代码,则需要遵循正常的内存管理规则,包括使用CFRetain()
和CFRelease()
。As the documentation states,
CFSTR()
created strings remain valid until the program terminates. You can release them all day long, but they won't actually be deallocated. For that reason, there's no need to explicitly retain/release them. It's valid to retain/release them because otherwise, you couldn't pass them around through other code that retains/releases them (framework methods, etc). Treat them like you would NSString literals created using@""
, that is to say, no need to retain or release them after creation, but if you're writing code that can take any CFString, you need to follow normal memory management rules, including usingCFRetain()
andCFRelease()
.