ARC是否会自动释放Core Foundation对象,还是需要手动进行内存管理? [英] Does the Core Foundation objects are automatically released by ARC or do we need manual memory management?

本文介绍了ARC是否会自动释放Core Foundation对象,还是需要手动进行内存管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,正在创建一个 Core Foundation 对象,从苹果文档中我就知道了

"Core Foundation对象的寿命由其确定 参考计数" 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对象.

我刚遇到类似这样的声明

回想一下,ARC仅处理Objective-C对象.没关系 保留和释放不是的CoreFoundation对象 Objective-C对象. http://www.raywenderlich.com/23037/如何在xcode中使用仪器

因此,如果有人遇到相同的问题并找到了可以共享的解决方案...

先谢谢了.

解决方案

您必须调用CFRelease才能释放Core Foundation对象.

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

编译器不会自动管理Core Foundation对象的生存期.您必须呼叫CFRetainCFRelease.

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

  • __bridge_transferCFBridgingRelease将非Objective-C指针移动到Objective-C,还将所有权转移到ARC.
    • ARC负责放弃对象的所有权.

因此,在以下情况下,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);

In my code am creating a Core Foundation object, and from the apple documentation i came to know that

"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

So I highly doubt that whether the core foundation objects are released by ARC or do we need to release by writing CFRelease(myobject)

Am using Xcode 6.4,and presently in my code am not using any CFRelease(myobject) for releasing my Core Foundation objects,and still I couldn't find any memory leak in xcode instruments(Leak)..

So my question is whether ARC will take care of releasing Core Foundation objects..??

as I just came across a statement like,

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

So if any one came across the same problem and found a solution pls do share...

Thanks in advance..

解决方案

You must call CFRelease to release Core Foundation object.

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.

Or you can use __bridge_transfer or CFBridgingRelease to move the ownership of the Core Foundation object to the under the Objective-C ARC object ownership.

  • __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.

So the following case, the NSString* __strong name variable has the ownership of the Core Foundation object. The Core Foundation object is automatically released when name = nil; or the end of the scope of name variable.

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

Or

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

这篇关于ARC是否会自动释放Core Foundation对象,还是需要手动进行内存管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆