It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                        
                    
                
            
                7年前关闭。
        

    

我很难在以下代码中放置CFRelease()调用。如果我将CFRelease()放在一个括号中,它将抱怨在另一个括号中丢失。

ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

if (phones == nil || ABMultiValueGetCount(phones) == 0) {

    CFArrayRef linkedContacts = ABPersonCopyArrayOfAllLinkedPeople(person);
    phones = ABMultiValueCreateMutable(kABPersonPhoneProperty);

    for (int i = 0; i < CFArrayGetCount(linkedContacts); i++) {

        ABRecordRef linkedContact = CFArrayGetValueAtIndex(linkedContacts, i);
        ABMultiValueRef linkedPhones = ABRecordCopyValue(linkedContact, kABPersonPhoneProperty);

        if (linkedPhones != nil && ABMultiValueGetCount(linkedPhones) > 0) {

            for (int j = 0; j < ABMultiValueGetCount(linkedPhones); j++) {

                ABMultiValueAddValueAndLabel(phones, ABMultiValueCopyValueAtIndex(linkedPhones, j), NULL, NULL);
            }
        }
    }

    if (ABMultiValueGetCount(phones) == 0) {

        return NO;
    }
}

最佳答案

如您所知,您必须释放“拥有”的所有对象,即所有对象
从名称中带有“创建”或“复制”的函数返回,但仅当调用
成功了。如果函数返回NULL,则不得在返回的值上调用CFRelease

例如,在您的代码中

ABMultiValueRef linkedPhones = ABRecordCopyValue(linkedContact, kABPersonPhoneProperty);
if (linkedPhones != nil && ABMultiValueGetCount(linkedPhones) > 0) {
    // ...
}


不清楚是否在if块的末尾调用CFRelease(linkedPhones)
最好单独检查呼叫是否成功。

因此,您的代码部分应如下所示:

ABMultiValueRef linkedPhones = ABRecordCopyValue(linkedContact, kABPersonPhoneProperty);
if (linkedPhones != nil) {
    for (int j = 0; j < ABMultiValueGetCount(linkedPhones); j++) {
        CFTypeRef value = ABMultiValueCopyValueAtIndex(linkedPhones, j);
        if (value != nil) {
            ABMultiValueAddValueAndLabel(phones, value, NULL, NULL);
            CFRelease(value);
        }
    }
    CFRelease(linkedPhones);
}


我希望这将使您开始重写分析仪安全的完整功能!

09-27 23:15