gpt4 book ai didi

memory-management - Core Foundation 对象是由 ARC 自动释放还是需要手动内存管理?

转载 作者:行者123 更新时间:2023-12-01 22:20:52 25 4
gpt4 key购买 nike

在我的代码中创建一个Core Foundation对象,并且从苹果文档中我了解到

"The life span of a Core Foundation object is determined by its reference count" https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Articles/lifecycle.html

所以我非常怀疑核心基础对象是通过ARC释放还是需要通过写CFRelease(myobject)来释放

我正在使用 Xcode 6.4,目前在我的代码中没有使用任何 CFRelease(myobject) 来释放我的 Core Foundation 对象,但我仍然在 xcode 中找不到任何内存泄漏仪器(泄漏)..

所以我的问题是 ARC 是否会负责释放 Core Foundation 对象..??

因为我刚刚看到这样的声明,

Recall that ARC only deals with Objective-C objects. It doesn’t manage the retain and release of CoreFoundation objects which are not Objective-C objects.http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode

因此,如果有人遇到同样的问题并找到解决方案,请分享...

提前致谢..

最佳答案

您必须调用CFRelease来释放Core Foundation对象。

https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW1

The compiler does not automatically manage the lifetimes of Core Foundation objects; you must call CFRetain and CFRelease.

或者您可以使用 __bridge_transferCFBridgingRelease 将 Core Foundation 对象的所有权移至 Objective-C ARC 对象所有权下。

  • __bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.
    • ARC is responsible for relinquishing ownership of the object.

因此,在下面的情况下,NSString* __strong name 变量拥有 Core Foundation 对象的所有权。当 name = nil;name 变量范围结束时,Core Foundation 对象会自动释放。

NSString *name = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

或者

NSString *name = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

关于memory-management - Core Foundation 对象是由 ARC 自动释放还是需要手动内存管理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com