gpt4 book ai didi

c++ - CFRelease 导致崩溃

转载 作者:行者123 更新时间:2023-11-30 03:54:45 24 4
gpt4 key购买 nike

以下代码导致我的 C++ 应用程序崩溃:

CFMutableDictionaryRef property_dictionary = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );

if ( ! property_dictionary )
break;

CFDictionarySetValue( property_dictionary, CFSTR( "somekey" ), CFSTR("someval") );
CFMutableDictionaryRef match_dictionary = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );

if ( ! match_dictionary )
break;

CFDictionarySetValue( match_dictionary, CFSTR(kIOPropertyMatchKey), property_dictionary );

io_iterator_t service = IOServiceGetMatchingService( kIOMasterPortDefault, match_dictionary );

if ( property_dictionary != NULL )
CFRelease( property_dictionary );

// the following bit causes crash
if ( match_dictionary != NULL )
CFRelease( match_dictionary );

不知IOServiceGetMatchingService是否与此有关。

最佳答案

IOServiceGetMatchingService() 在内存管理方面很特殊。它使用对传入字典的一个引用。由于您的代码只有一个引用,它在调用后不再拥有 match_dictionary 字典,并且不得对其调用 CFRelease()

来自docs :

matching A CF dictionary containing matching information, of which one reference is always consumed by this function…

关于c++ - CFRelease 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com