2012-07-05 89 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存在。

相關問題