AddressBook iOS中的内存泄漏问题

时间:2014-01-18 06:43:03

标签: ios memory-leaks addressbook

从地址簿中获取联系人详细信息时,我遇到内存泄漏问题。以下是我获取详细信息的代码。任何人都可以帮助我吗?

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
               }
           });
    }

1 个答案:

答案 0 :(得分:1)

我可以看到这一行。

ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);

所以你应该在使用之后释放,如下所示,

CFRelease(addressBook);