Управление памятью CFErrorRef, возвращаемое ABRecordSetValue - PullRequest
1 голос
/ 25 декабря 2011

Рассмотрим типичный код CF, включающий обработку ошибок, скажем что-то вроде этого:

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

Как мне обработать anError после этого кода? Должен ли я сохранить его, чтобы он не исчез, а потом отпустить? Или я уже владелец, и мне нужно только выпустить его позже?

Ответы [ 2 ]

9 голосов
/ 25 декабря 2011

В платформе Core Foundation вызывающий всегда обязан выпустить ошибку, возвращаемую через аргумент CFErrorRef *. Например, вот комментарий к заголовочному файлу от 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. */

Скорее всего, AB Framework использует то же соглашение.

2 голосов
/ 01 ноября 2013

Согласно "CFError.h", где определен CFErrorRef: то есть

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

если вы прокрутите вверх, вы увидите это в строке 14 до строки 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

Таким образом, нам кажется, что нам нужно самим выпустить CFErrorRef!

...