Why would CFRelease(NULL) crash?

前端 未结 4 2113
攒了一身酷
攒了一身酷 2020-12-17 09:31

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?

4条回答
  •  青春惊慌失措
    2020-12-17 10:05

    All of these functions are part of different APIs that follow different conventions with regards to handling NULL:

    1. CFRelease is part of the CoreFoundation C SDK, which does not accept NULL reference as arguments by default.
    2. [nil release] uses Objective-C (which allows dereferences of nil)
    3. free(NULL) is part of C library (libc) which permits NULL arguments
    4. delete NULL is part of the C++ library (libc++) which permits NULL arguments

    I guess the CoreFoundation SDK writers decided to be more consistent with the rest of SDK rather than with similar function in other SDKs.

提交回复
热议问题