CFStringRef到NSString ARC泄漏.为什么? [英] CFStringRef to NSString ARC leaking. Why?
问题描述
我一直在寻找在ARC中从CFStringRef
转到NSString
的正确方法,以避免内存泄漏,并且一些主要的投票建议表明:
I have been looking around for the correct way to go from CFStringRef
to NSString
in ARC to avoid memory leaks and some of the main voted answers suggest:
NSString * string = (__bridge NSString *)cfString;
我在这里使用这种方法,但是在对应用程序进行性能分析时,这种小方法仍然会导致内存泄漏.(见附图).
I am using that approach here but when profiling the app I still get a memory leak in this small method [see attached image].
因此,我不确定如何解决此问题.有人有解决这个问题的方法吗?
So, I am not sure how to fix this. Anybody has the solution to this problem?
谢谢
因此,显然在返回之前添加CFRelease(ext)
可以修复泄漏.问题是我不完全理解原因.我以为那行:
So, apparently adding the CFRelease(ext)
before the return fixed the leak. Problem is I don't think I fully understand the reason. I thought that the line:
NSString * extension = (__bridge NSString*)ext
将拥有Core Foundation ext字符串的所有权并处理发布. 有人能确切解释这里发生了什么吗?
Would take ownership of the Core Foundation ext string and handle the release. Can anybody explain what is going on here exactly?
推荐答案
按照事实上的标准"可可命名约定,名称中包含Create
或Copy
的函数将返回带有引用计数的对象of1.您必须将此参考计数转移到ARC-land,以便ARC可以处理它.为此,请使用__bridge_transfer
关键字.
As per the de-facto "standard" Cocoa naming convention, functions that contain Create
or Copy
in their name return an object with a reference count of 1. You have to transfer this reference count into ARC-land so that ARC can take care of it. You use the __bridge_transfer
keyword for this purpose.
NSString *string = (__bridge_transfer NSString *)cfString;
这篇关于CFStringRef到NSString ARC泄漏.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!