Memory management of a CFErrorRef returned by ABRecordSetValue

烈酒焚心 提交于 2019-12-01 01:27:28

In the Core Foundation framework, it's always the caller's responsibility to release an error returned through a CFErrorRef * argument. For example here's a header file comment from CFBundle.h:

CF_EXPORT
Boolean CFBundlePreflightExecutable(CFBundleRef bundle, CFErrorRef *error) CF_AVAILABLE(10_5, 2_0);
    /* This function will return true if the bundle is loaded, or if the bundle appears to be */
    /* loadable upon inspection.  This does not mean that the bundle is definitively loadable, */
    /* since it may fail to load due to link errors or other problems not readily detectable. */
    /* If this function detects problems, it will return false, and return a CFError by reference. */
    /* It is the responsibility of the caller to release the CFError. */

Chances are the AB framework uses the same convention.

According to "CFError.h" where CFErrorRef is defined: i.e.

typedef struct __CFError * CFErrorRef; // line 43 in CFError.h

if you scroll to the top, you will see this in line 14 to line 22:

CFError *error;
if (!ReadFromFile(fd, &error)) {
    ... process error ...
    CFRelease(error);   // If an error occurs, the returned CFError must be released.
}

It is the responsibility of anyone returning CFErrors this way to:
- Not touch the error argument if no error occurs
- Create and assign the error for return only if the error argument is non-NULL

Thus it seems like we do need to release the CFErrorRef ourselves!

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