【问题标题】:CFStringRef release gives bad access when it is nilCFStringRef 版本为 nil 时提供了错误的访问权限
【发布时间】:2015-12-24 10:25:01
【问题描述】:
ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for (int i=0; i < ABMultiValueGetCount(phonesRef); i++)
    {
        CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
        CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);

        if (currentPhoneLabel != nil && currentPhoneValue != nil)
        {
            if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo)
            {
                [contactInfoDict setObject:(__bridge NSString *)currentPhoneValue forKey:@"workNumber"];
            }

            if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo)
            {
                [contactInfoDict setObject:(__bridge NSString *)currentPhoneValue forKey:@"homeNumber"];
            }
        }
        else if (currentPhoneValue != nil && currentPhoneLabel == nil)
        {
            [contactInfoDict setObject:(__bridge NSString *)currentPhoneValue forKey:@"workNumber"];
        }

        CFRelease(currentPhoneLabel);
        CFRelease(currentPhoneValue);
    }
    CFRelease(phonesRef);

这是我将联系人电话导入我的 ios 应用程序的代码,但是当 currentPhoneLabel 为 nil 时,CFRelease(currentPhoneLabel) 的 xocde。 我不知道为什么会这样。 任何帮助都会非常显着。

谢谢

【问题讨论】:

  • 我从下面的 stackoverflow 中参考了关于内存释放的答案,根据您的崩溃日志,如果该标签中没有数据,它如何释放内存?它应该是崩溃吧? stackoverflow.com/questions/17959760/…

标签: ios objective-c xcode7 addressbook cfstring


【解决方案1】:

您已经在代码中的其他地方设置了守卫,因此您只需在每次调用 CFRelease() 时也添加一个守卫:

if (currentPhoneLabel != NULL)
    CFRelease(currentPhoneLabel);
// etc.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多