释放 Core Foundation 对象引用

发布于 2024-07-19 06:42:12 字数 294 浏览 4 评论 0原文

我是否需要释放 Core Foundation 对象来清理内存? 如果是这样,怎么办?

例如代码中:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);

我需要释放peopleArray吗? addressBook 怎么样?

Do I need to release a Core Foundation objects to clear up memory? And if so, how?

For example, in the code:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);

do I need to release peopleArray? What about addressBook?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

寂寞美少年 2024-07-26 06:42:12

是的,在 CoreFoundation 中,您必须发布名称中带有 Create 或 Copy 的任何内容。 您可以使用 CFRelease() 来完成此操作。 就您而言,您应该释放数组和地址簿引用。

Yes, in CoreFoundation you have to release anything with Create or Copy in the name. You do this with CFRelease(). In your case, you should be releasing both the array and the address book references.

深爱成瘾 2024-07-26 06:42:12

规则 Core Foundation 中的内存管理与 Cocoa 中的类似:如果返回引用的方法包含“create”或“copy”一词,则您拥有该引用,并且必须调用 CFRelease()提到放弃所有权。 否则,您拥有该引用,并且必须调用 CFRetain 来获取所有权(必然需要后续的 CFRelease 来放弃该新所有权)。 这些规则取自《Core Foundation 内存管理编程指南》


  • 直接或通过复制
    另一个对象——参见“创建
    规则”),你拥有它。
  • 如果你得到一个
    来自其他地方的反对,你这样做
    不拥有它。 如果你想阻止它
    被处置时,您必须添加
    你自己作为所有者(使用
    CF保留)。
  • 如果您是业主
    对象,您必须放弃所有权
    当你用完它时
    (使用 CFRelease)。

在您的示例中,必须释放 addressBookpeopleArray。 由于 Core Foundation 中没有等效的 autorelease 功能,因此如果您从方法返回引用,请返回数组而不释放它。 然后,您应该(除非您是邪恶的)在方法名称中包含“create”,以向调用者表明他们现在拥有对返回对象的引用。 在这种情况下,CFArray 与 NSCFArray 是免费桥接的,NSCFArray 是一个继承自 NSObject 的 Objective-C 对象。 因此,如果您需要从函数/方法返回它,您可以将 peopleArray 转换为 NSArray*autorelease

return [(NSArray*)peopleArray autorelease];

请注意,这只适用于免费桥接课程。 我的理解是,创建自己的免费桥接类相当困难,并且只有原始(字符串、数组等)CF 类是免费桥接的,因此这种方法并不总是有效。 最后,如果您可以避免使用 autorelease(即您可以使内存管理更加明确),那可能总是一件好事。

The rules for memory management in Core Foundation are similar to those in Cocoa: if the method that returns a reference contains the words "create" or "copy", you own the reference and must call CFRelease() on that reference to relinquish ownership. Otherwise, you do not own the reference and must call CFRetain to take ownership (necessarily requiring a subsequent CFRelease to relinquish that new ownership). These rules, as taken from the Memory Management Programming Guide for Core Foundation are:

  • If you create an object (either
    directly or by making a copy of
    another object—see “The Create
    Rule”), you own it.
  • If you get an
    object from somewhere else, you do
    not own it. If you want to prevent it
    being disposed of, you must add
    yourself as an owner (using
    CFRetain).
  • If you are an owner of an
    object, you must relinquish ownership
    when you have finished using it
    (using CFRelease).

In your example, both the addressBook and the peopleArray must be released. Since there is no autorelease equivalent in Core Foundation, if you are returning the a reference from a method, return the array without releasing it. You should (unless you're being evil) then include "create" in the method name to indicate to the caller that they now own a reference to the returned object. In this case, CFArray is toll-free bridged to NSCFArray, an Objective-C object that inherits from NSObject. You can thus cast peopleArray to an NSArray* and autorelease that, if you need to return it from a function/method:

return [(NSArray*)peopleArray autorelease];

Note that this only works for toll-free bridged classes. My understanding is that it's rather difficult to make your own toll-free bridged classes and only the primitive (string, array, etc.) CF classes are toll-free bridged, so this approach won't work always. Finally, if you can avoid using autorelease (i.e. you can make your memory management more explicit), that's probably always a good thing.

↙厌世 2024-07-26 06:42:12

另一个没有人提到的小问题是,一些 CF 类别与 NS 对应类别之间有一个“免费桥梁”。 CFStringNSStringCFArrayNSArray 都是示例。 这是相关的,因为您可以将 release 与这些类一起使用。

请参阅此其他 StackOverflow 问题了解更多信息。

Another small point that no-one has mentioned yet, some CF classes have a "toll-free bridge" with their NS counterpart. CFString and NSString, CFArray and NSArray are both examples. This is relevant as you can just use release with these classes.

See this other StackOverflow question for more information.

独木成林 2024-07-26 06:42:12

我建议阅读Apple关于Core Foundation内存管理的指南对此进行了深入的讨论。 他们也有一个类似的通用 Cocoa 内存管理指南。

要释放 CF 对象,您可以调用 CFRelease 函数。

I would suggest reading Apple's guide on Core Foundation memory management for an in-depth discussion of this. They have a similar guide for general Cocoa memory management as well.

To release a CF object, you would call the CFRelease function.

野鹿林 2024-07-26 06:42:12

对于 ARC,您可以使用。

return (__bridge_transfer NSArray*)peopleArray;

“__bridge_transfer”会将所有权转移给 ARC,因此您不需要任何进一步的发布调用。

For ARC you can use.

return (__bridge_transfer NSArray*)peopleArray;

"__bridge_transfer" will transfer the ownership to ARC and therefore you don't need any further release call.

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