【问题标题】:CFStringCreateWithCString is paired with a CFRelease,but it crashedCFStringCreateWithCString 与 CFRelease 配对,但它崩溃了
【发布时间】:2015-07-09 08:05:18
【问题描述】:

我在 for 循环中放置了一对 CFStringCreateWithCString 和 CFRelease

    for (NSInteger i = 0; i < tempDict.count; i ++) {
        NSString *key = tempDict.allKeys[i];
        NSString *numStr = tempDict[key];
        CFStringRef numValue = CFStringCreateWithCString(kCFAllocatorDefault, [numStr UTF8String], kCFStringEncodingUTF8);
        if (!ABMultiValueAddValueAndLabel(phone, numValue, kABPersonPhoneMainLabel, NULL)) {
            DLog(@"add multivalue failed. index: %i", i);
        }
        CFRelease(numValue);
    }

CFRelease 导致我的应用程序崩溃,并且控制台记录了:

*** -[CFString release]: message sent to deallocated instance 0x14fa4410

如果我注释掉 CFRelease,它可以正常工作。但崩溃不像 EXC_BAD_ACCESS,它是这样的:

但是当我点击它下面的两行时

它告诉我错误在这里,这个代码块的结尾

我的问题是:

1,为什么它会因“-[CFString release]: message sent to deallocated instance 0x17065170”错误而崩溃?在 CoreFoundation 中,create 和 release 函数应该是配对的,不是吗?

2、为什么编译器认为错误在最后一行?首先,不是 ABAddressBookRef,不是 CFString,并且控制台日志很清楚:-[CFString release]: message sent to deallocated instance,其次,应用程序在我在前面的 for 循环中注释 CFRelease 之后立即运行,所以我认为如果有错误,无论如何都不会出现在 CFRelease(addressBook) 中。

3,这个错误是什么意思?这是一个 EXC_ARM_BREAKPOINT,但我们看到很多“发送到已释放实例的消息”错误标记为 EXC_BAD_ACCESS。

希望有人可以帮助我。提前致谢!

【问题讨论】:

    标签: ios objective-c core-foundation


    【解决方案1】:

    无论如何您都不需要创建CFString 对象,因为您可以在NSStringCFStringRef 之间建立桥梁:

    for (NSString *key in [tempDict allKeys]) {
            NSString *numStr = tempDict[key];
            if (!ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)numStr, kABPersonPhoneMainLabel, NULL)) {
                DLog(@"add multivalue failed. index: %i", i);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多