且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有内存泄漏的情况下返回CFDataRef?

更新时间:2022-03-19 21:26:56

您无法自动释放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.