CFSTR内存管理

发布于 2025-01-05 02:25:01 字数 424 浏览 2 评论 0原文

我正在使用 CFSTR 函数从常量 c 字符串创建 CFString,并且我在守护进程中非常频繁地调用此函数。

来自文档
CFSTR 返回的值具有以下语义:

  1. 从 CFSTR 返回的值不是由 CFString 释放的 - 它们是 保证有效直到程序终止
  2. 你可以保留 并以平衡的方式释放从 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:

  1. Values returned from CFSTR are not released by CFString—they are
    guaranteed to be valid until the program terminates.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2025-01-12 02:25:01

正如文档所述,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 using CFRetain() and CFRelease().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文