I have this code that enumerates address book contacts -
+(void)enumerateAddressBookContacts:(void(^)(NSString *name, NSArray *phoneNumbers, NSArray *emailAddresses, NSData *imageData, NSString *recordId))enumerationBlock failure:(void (^)( NSError *error))failure{
[self requestAddressBookPermissionsWithCompletion:^{
if (enumerationBlock)
{
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);
NSArray* people = (__bridge NSArray*)addressBookPeople;
if ([people count])
{
for (NSUInteger i = 0; i < [people count]; i++)
{
ABRecordRef person = (__bridge ABRecordRef)people[i];
NSArray *phoneNumbers = [self phoneNumbersOfPerson:person];
NSArray *emailAddresses = [self emailAddressesOfPerson:person];
NSString *fullName = [self fullNameOfPerson:person];
NSData *imageData = [self imageDataOfPerson:person];
NSString *recordId = [NSString stringWithFormat:@"%d", ABRecordGetRecordID(person)];
void (^block)(NSString*, NSArray *, NSArray *, NSData*, NSString*) = [enumerationBlock copy];
block(fullName,phoneNumbers,emailAddresses,imageData,recordId);
}
}
else
{
//No contacts in addressbook
if (failure) {
NSError *error = [NSError errorWithDomain:kAddressBook code:kAddressBookUploadFailReasonEmptyAddressBook userInfo:@{NSLocalizedDescriptionKey:kAddressBookUploadFailReason[kAddressBookUploadFailReasonEmptyAddressBook]}];
failure(error);
}
}
if (addressBookPeople)
{
CFRelease(addressBookPeople);
}
if (addressBook)
{
CFRelease (addressBook);
}
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
I have a crash on the release of the ABAddressBookRef
object at the end of the method.
This is an example of one of the enumeration method (They all act the same) -
+ (NSArray*)phoneNumbersOfPerson:(ABRecordRef)person
{
ABMutableMultiValueRef phones = ABRecordCopyValue (person, kABPersonPhoneProperty);
NSMutableArray *phoneNumbers = [NSMutableArray array];
CFIndex phonesNumberCount = ABMultiValueGetCount (phones);
if (phonesNumberCount > 0)
{
for (CFIndex index = 0; index < phonesNumberCount; index++)
{
CFStringRef phoneValue = ABMultiValueCopyValueAtIndex (phones, index);
[phoneNumbers addObject:(__bridge_transfer NSString *)phoneValue];
if (phoneValue)
{
CFRelease (phoneValue);
}
}
}
if (phones)
{
CFRelease (phones);
}
return phoneNumbers;
}
When I remove the release I don't crash but I assume I will have a leak. Any idea what could be the reason ?
Thanks