为什么 CFRelease(NULL) 会崩溃? [英] Why would CFRelease(NULL) crash?

查看:135
本文介绍了为什么 CFRelease(NULL) 会崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CFRelease 不检查 NULL 是否有原因?[nil release] 的时候不是不能接受吗?自由(空);删除空;一切正常?

Is there a reason why CFRelease does not check for NULL? Isn't it unacceptable when [nil release]; free(NULL); delete NULL; all work perfectly fine?

推荐答案

CoreFoundation 的源代码是公开的.具体来说,对于 Snow Leopard,CFRelease 的代码位于 http://www.opensource.apple.com/source/CF/CF-550/CFRuntime.c

The source code to CoreFoundation is publicly available. Specifically, for Snow Leopard the code to CFRelease is in http://www.opensource.apple.com/source/CF/CF-550/CFRuntime.c

以下是相关部分的样子:

Here is what the relevant portion looks like:

void CFRelease(CFTypeRef cf) {
    if (NULL == cf) HALT;
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
    if (CF_IS_COLLECTABLE(cf)) {
        if (CFTYPE_IS_OBJC(cf)) {
            // release the GC-visible reference.
            auto_zone_release(auto_zone(), (void*)cf);
        } else {
            // special-case CF objects for better performance.
            _CFRelease(cf);
        }
        return;
    }
#endif
}

这并没有回答您关于设计动机的问题,但您还询问了为什么 CFRelease 不检查 NULL.它确实会检查,但在将 NULL 作为参数传递时故意失败.

This doesn't answer your question about design motivations, but you also asked why CFRelease does not check for NULL. It does check, and fails on purpose when NULL is passed as the parameter.

我个人的信念与 Quinn 的相似——CF 设计者认为传递 NULL 是一个编程错误.

My personal belief is similar to Quinn's- that the CF designers felt it is a programming error to pass NULL.

这篇关于为什么 CFRelease(NULL) 会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆