Reputation: 65
I'm importing contacts from address book, i got this error when running in iPhone. It's perfectly working in simulator. I got this error when releasing name object. When i comment this CFRelease((__bridge CFTypeRef)(name)); its working fine in device also. In Xcode it showing a memory leak but no crash in device. Please find my code also.
// 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);
Upvotes: 1
Views: 3877
Reputation: 122458
Use __bridge_transfer
instead and let ARC worry about it (or bits of it):
// 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);
Upvotes: 1