释放 Core Foundation 对象引用
我是否需要释放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,在 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.
规则 Core Foundation 中的内存管理与 Cocoa 中的类似:如果返回引用的方法包含“create”或“copy”一词,则您拥有该引用,并且必须调用
CFRelease()
提到放弃所有权。 否则,您不拥有该引用,并且必须调用CFRetain
来获取所有权(必然需要后续的CFRelease
来放弃该新所有权)。 这些规则取自《Core Foundation 内存管理编程指南》直接或通过复制
另一个对象——参见“创建
规则”),你拥有它。
来自其他地方的反对,你这样做
不拥有它。 如果你想阻止它
被处置时,您必须添加
你自己作为所有者(使用
CF保留)。
对象,您必须放弃所有权
当你用完它时
(使用 CFRelease)。
在您的示例中,必须释放
addressBook
和peopleArray
。 由于 Core Foundation 中没有等效的autorelease
功能,因此如果您从方法返回引用,请返回数组而不释放它。 然后,您应该(除非您是邪恶的)在方法名称中包含“create”,以向调用者表明他们现在拥有对返回对象的引用。 在这种情况下,CFArray 与NSCFArray
是免费桥接的,NSCFArray 是一个继承自NSObject
的 Objective-C 对象。 因此,如果您需要从函数/方法返回它,您可以将peopleArray
转换为NSArray*
和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 callCFRetain
to take ownership (necessarily requiring a subsequentCFRelease
to relinquish that new ownership). These rules, as taken from the Memory Management Programming Guide for Core Foundation are:directly or by making a copy of
another object—see “The Create
Rule”), you own it.
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).
object, you must relinquish ownership
when you have finished using it
(using CFRelease).
In your example, both the
addressBook
and thepeopleArray
must be released. Since there is noautorelease
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 toNSCFArray
, an Objective-C object that inherits fromNSObject
. You can thus castpeopleArray
to anNSArray*
andautorelease
that, if you need to return it from a function/method: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.另一个没有人提到的小问题是,一些 CF 类别与 NS 对应类别之间有一个“免费桥梁”。
CFString
和NSString
、CFArray
和NSArray
都是示例。 这是相关的,因为您可以将release
与这些类一起使用。请参阅此其他 StackOverflow 问题了解更多信息。
Another small point that no-one has mentioned yet, some CF classes have a "toll-free bridge" with their NS counterpart.
CFString
andNSString
,CFArray
andNSArray
are both examples. This is relevant as you can just userelease
with these classes.See this other StackOverflow question for more information.
我建议阅读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.对于 ARC,您可以使用。
“__bridge_transfer”会将所有权转移给 ARC,因此您不需要任何进一步的发布调用。
For ARC you can use.
"__bridge_transfer" will transfer the ownership to ARC and therefore you don't need any further release call.