Question

I am facing memory leak issue while fetching contact details from Address book. Below is my code for fetching details. Can anyone help me?

NSMutableDictionary *mycontacts = [[NSMutableDictionary alloc] init];
ABAddressBookRef addressBookRef =ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() ==kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
         {
            if (granted)
               {
                   ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);
           //###MEMORY LEAK LINE
                    NSArray * allPeople = (__bridge NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
                    CFIndex numberOfPeople =ABAddressBookGetPersonCount(addressBook);
                    for( NSUInteger i = 0; i < numberOfPeople; i++)
                    {
                       ABRecordRef person =CFArrayGetValueAtIndex((__bridge CFArrayRef)(allPeople), i );
                        NSString *mobileNumber =[[NSString alloc]init];
                        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person,kABPersonFirstNameProperty));
                        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
                        for (CFIndex i = 0; i <ABMultiValueGetCount(phoneNumbers); i++)
                        {
                            NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                            if(phoneNumber.length>0)
                            {
                                mobileNumber=phoneNumber;
                                mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
                                mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
                                mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
                                mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
                                NSString *unfilteredString = mobileNumber;
                                NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
                                NSString*updatedmobileNumber = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars]componentsJoinedByString:@""];

                                [mycontacts setObject:updatedmobileNumber forKey:firstName];
                                break;
                            }
                            if(phoneNumber !=nil)
                            {
                                CFRelease((__bridge CFTypeRef)(phoneNumber));
                            }

                        }
                        if(phoneNumbers !=nil)
                        {
                            CFRelease(phoneNumbers);
                        }

                        if(person !=nil)
                        {
                            CFRelease(person);
                        }

                        if(firstName !=nil)
                        {
                            CFRelease((__bridge CFTypeRef)(firstName));
                        }
                    }
                   if(allPeople !=nil)
                   {
                       CFRelease((__bridge CFTypeRef)(allPeople));
                   }
               }
               else
               {
                       // User denied access
                       // Display an alert telling user the contact could not be added
               }
           });
    }
Was it helpful?

Solution

I can see this line.

ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);

So you should release after using this as below,

CFRelease(addressBook);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top