Memory management of a CFErrorRef returned by ABRe

2019-02-15 21:33发布

问题:

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!