0

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

shannoga
  • 19,649
  • 20
  • 104
  • 169

3 Answers3

0

Well found the problem and I believe it could help other users so -

Looks like (__bridge_transfer NSString *) already decrements the retain count of the CFStringRef, So there is no need to release it.

Looks like this is no true for __bridge

shannoga
  • 19,649
  • 20
  • 104
  • 169
0

I would also suggest to check the result for those 2 functions:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);

Any of those two can return NULL, and trying to access it will result in crash.

Cherpak Evgeny
  • 2,659
  • 22
  • 29
0

You are not showing what your function requestAddressBookPermissionsWithCompletion does. I'll paste part of the code I am using in my Addressbook manager:

//Saving current thread queue.
dispatch_queue_t currentQueue = dispatch_get_global_queue(0,0);
CFErrorRef error = nil;
addressBook = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    // callback can occur in background, address book must be accessed on thread it was created on
    dispatch_async(currentQueue, ^{
        if(error || !granted){

            //SOME ERROR HAPPENED OR USER DIDN'T ALLOW ACCESSING CONTACTS -> SHOW SOME FEEDBACK
        }
        else {
            // DO WHATEVER YOU NEED WITH THE ADDRESSBOOK

        }
        CFRelease(addressBook);
    });
});

I can release addressbook without any problem

Eli Kohen
  • 521
  • 4
  • 8