我正在从地址簿导入联系人,在 iPhone 中运行时出现此错误。它在模拟器中完美运行。释放名称对象时出现此错误。当我评论此 CFRelease((__bridge CFTypeRef)(name)); 它在设备中也能正常工作。在 Xcode 中,它显示内存泄漏但设备没有崩溃。也请找到我的代码。
// Create an new address book object with data from address book database
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// Returns all the records in address book and store in array
NSArray * allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
// Allocate memory for appdelegare mutable array to store contacts as dictinary object
appDelegate.allPeopleDict = [[NSMutableArray alloc]init];
for (id person in allPeople) {
// Get name of person from address book contacts
NSString * name = (__bridge NSString *)(ABRecordCopyCompositeName((__bridge ABRecordRef)(person)));
// Get phone numbers of person from address book contacts
ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
// Create a mutable array to store phone numbers
NSMutableArray * phoneNumbersArray = [[NSMutableArray alloc]init];
NSString * phoneNumber;
for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
// Get each phone number of a person
phoneNumber = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
// Add the phone number to phone numbers array and trim the phone number
[phoneNumbersArray addObject:[self trimThePhoneNumber:phoneNumber]];
// Release phone number object
CFRelease((__bridge CFTypeRef)(phoneNumber));
}
// Create a dictionary object with name and phonenumbers
NSDictionary * personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,phoneNumbersArray, nil];
//NSDictionary * personDict = [[NSDictionary alloc]initWithObjectsAndKeys:name,[self trimThePhoneNumber:phoneNumber], nil];
CFRelease(phones);
// Add this dictionary object to mutable array
[appDelegate.allPeopleDict addObject:personDict];
// Release name object
CFRelease((__bridge CFTypeRef)(name));// Here i'm getting this error
}
// Release allPeople array object and addressbook object
CFRelease((__bridge CFTypeRef)(allPeople));
CFRelease(addressBook);
最佳答案
改用 __bridge_transfer
并让 ARC 担心它(或它的一部分):
// Create an new address book object with data from address book database
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// Returns all the records in address book and store in array
NSArray * allPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
// Allocate memory for appdelegare mutable array to store contacts as dictinary object
appDelegate.allPeopleDict = [[NSMutableArray alloc]init];
for (id person in allPeople) {
// Get name of person from address book contacts
NSString * name = (__bridge_transfer NSString *)(ABRecordCopyCompositeName((__bridge ABRecordRef)(person)));
// Get phone numbers of person from address book contacts
ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
// Create a mutable array to store phone numbers
NSMutableArray * phoneNumbersArray = [[NSMutableArray alloc]init];
for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
// Get each phone number of a person
NSString * phoneNumber = (__bridge_transfer NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
// Add the phone number to phone numbers array and trim the phone number
[phoneNumbersArray addObject:[self trimThePhoneNumber:phoneNumber]];
}
// Create a dictionary object with name and phonenumbers
NSDictionary * personDict = @{
name : phoneNumbersArray
};
CFRelease(phones);
// Add this dictionary object to mutable array
[appDelegate.allPeopleDict addObject:personDict];
}
CFRelease(addressBook);
关于ios - 线程 3 : EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, 子代码 = 0xdefe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23365836/