Memory management of a CFErrorRef returned by ABRecordSetValue

 ̄綄美尐妖づ 提交于 2020-01-11 04:11:26

问题


Consider some typical CF code involving error handling, say something like this:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Joe"), &anError);

How do I handle anError after this code? Do I have to retain it, to make sure it doesn't go away, and then later release it? Or am I already the owner and I only have to release it later?


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/8627805/memory-management-of-a-cferrorref-returned-by-abrecordsetvalue

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