我从地址簿导入联系人,在iPhone中运行时遇到此错误。它在模拟器中完美运行。释放名称对象时出现此错误。当我评论这个 CFRelease((__ bridge CFTypeRef)(名称)); 它在设备中的工作正常。在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);
答案 0 :(得分:1)
使用__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);