iosobjective-cxcode7addressbookcfstring

CFStringRef release gives bad access when it is nil


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);

Here is my code for importing contact phones into my ios app, but when currentPhoneLabel is nil xocde for CFRelease(currentPhoneLabel). I don't know why is it happening. Any help would be much appreciable.

Thanks


Solution

  • You already have guards in other places in the code, so you just need to add a guard on every call to CFRelease() as well:

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