问题描述
当我通过
(CFDataRef)MyFunction{
.....
CFDataRef data = CFDataCreate(NULL, buf, bufLen);
free(buf);
return data;
}
内存泄漏,如何使CFDataRef
自动释放?方法[数据自动释放]不会退出.
There is a memory leak, how to make CFDataRef
autorelease?the method [data autorelease] doesn't exit.
推荐答案
您无法自动释放Core Foundation对象. (但是,您可以自动释放支持免费桥接的Core Foundation对象,例如CFDataRef;请参阅下面的@newacct答案.)
You can't autorelease Core Foundation objects. (However, you can autorelease Core Foundation objects that support toll-free bridging such as CFDataRef; see @newacct's answer below.)
Objective-C约定是为了命名您的方法,使其以单词new
开头,以指示调用方负责释放其返回值.例如:
The Objective-C convention is to name your method such that it starts with the word new
to indicate that the caller is responsible for releasing its return value. For example:
+ (CFDataRef)newDataRef {
return CFDataCreate(...);
}
CFDataRef myDataRef = [self newDataRef];
...
CFRelease(myDataRef);
如果遵守此命名约定,则Xcode静态分析器将正确标记Core Foundation内存管理问题.
If you conform to this naming convention, the Xcode static analyzer will correctly flag Core Foundation memory managment issues.
这篇关于如何在没有内存泄漏的情况下返回CFDataRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!