Does the Core Foundation objects are automatically released by ARC or do we need manual memory management?

自古美人都是妖i 提交于 2019-12-11 03:33:52

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/31434411/does-the-core-foundation-objects-are-automatically-released-by-arc-or-do-we-need

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!