0

我正在使用 ABPeoplePickerNavigationController 从通讯录条目中收集一组电子邮件地址和电话号码。90% 的时间它都能正常工作,但少数测试人员报告崩溃。崩溃报告说它在 CFRelease 崩溃......不知道为什么考虑到我相信我的代码是正确的。看一看:

ABProfile *selectedUser = [[ABProfile alloc]init];

ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

NSArray *emailArray;

if (ABMultiValueGetCount(emails) > 0) {
    emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emails);
}

CFRelease(emails);

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

NSMutableArray *phonesArray = [[NSMutableArray alloc]initWithCapacity:1];

for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:1];

    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);

    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
    NSString *phoneLabel =(__bridge_transfer NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

    CFRelease(locLabel);

    [dict setValue:phoneLabel forKey:@"label"];
    NSString *phoneNumber = (__bridge_transfer NSString *)phoneNumberRef;

    [dict setValue:phoneNumber forKey:@"number"];

    [phonesArray addObject:dict];
}

selectedUser.phones = phonesArray;

CFRelease(phones);
4

1 回答 1

0

ABMultiValueCopyLabelAtIndexNULL如果没有找到可以返回,并且CFRelease(NULL)会崩溃。在对它做任何事情之前,我会厚着脸皮确保locLabel存在。

于 2012-07-05T17:00:01.157 回答