ABPeoplePickerNavigationController EXC_BAD_ACCESS收集电子邮件和电话号码

时间:2012-07-05 16:52:14

标签: ios contacts exc-bad-access abrecord

我正在使用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);

1 个答案:

答案 0 :(得分:0)

如果找不到,则{p> ABMultiValueCopyLabelAtIndex可以返回NULLCFRelease(NULL)会崩溃。在做任何事情之前,我会确保locLabel存在。