2012-07-05 87 views
0

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

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

回答

0

ABMultiValueCopyLabelAtIndex可以返回NULL如果没有找到,而CFRelease(NULL)会崩溃。我会在做任何事情之前确保locLabel存在。

相关问题