ABRecordSetValue返回的CFErrorRef的内存管理

pf8*_*f85 1 c objective-c core-foundation

考虑一些涉及错误处理的典型CF代码,比如说:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Joe"), &anError);
Run Code Online (Sandbox Code Playgroud)

anError这段代码后如何处理?我是否必须保留它,以确保它不会消失,然后再释放它?或者我已经是主人了,我只需要稍后发布它?

rob*_*off 9

在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. */
Run Code Online (Sandbox Code Playgroud)

机会是AB框架使用相同的约定.

  • @AdamRosenfield,当文档说出创建规则后面的内容时,这意味着你应该解释它*好像它的名字中有"创建",即使它没有.它基本上意味着"你收到了这个".如果他们没有记录命名约定的异常,那么永远不会有理由在文档中调用这些东西(除了最初定义约定的地方之外). (2认同)