0

When i used build and analyse, i got leaks( it showed as potential leak of an object). to fix that i included as below

if ( aContactfirstName){
  CFRelease(aContactfirstName);
  }
if (aContactLastName){
  CFRelease(aContactLastName);
  }

But my app crashes.

So pls let me know where it leaks and solve it.

-(NSString*)getContactNameByPhoneNo:(NSString*)phoneNO{

      NSString *aContactName = phoneNO;
      ABAddressBookRef addressbook = ABAddressBookCreate();
      CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
      CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
      for (int i=0; i < numPeople; i++) { 
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex numPhones = ABMultiValueGetCount(phonelist);

        for (int j=0; j < numPhones; j++) {
          CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, j);
          NSString *personPhone = (NSString *)ABphone;
            NSLog(@"i am:");
          personPhone =[personPhone stringByReplacingOccurrencesOfString:@"-"withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@")"withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@" "withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@"("withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@"+"withString:@""];
            NSLog(@"xcxcxcxc");

          CFRelease(ABphone);

          if ( [personPhone isEqualToString:phoneNO] ){
            NSString *aContactfirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) ;
            NSString *aContactLastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) ;
            if ( aContactLastName != NULL && aContactfirstName != NULL){
              aContactName = [NSString stringWithFormat:@"%@ %@",aContactfirstName,aContactLastName];
            }
            else if(aContactfirstName != NULL){
              aContactName = aContactfirstName;
            }
            else if(aContactLastName != NULL){
              aContactName = aContactLastName;
            }

              if ( aContactfirstName){
                CFRelease(aContactfirstName);
              }
              if (aContactLastName){
                CFRelease(aContactLastName);
              }

            break;
          }
        }
        CFRelease(phonelist);
      }
      CFRelease(allPeople);
      CFRelease(addressbook);
      return aContactName;
    }
user198725878
  • 6,266
  • 18
  • 77
  • 135

2 Answers2

0

Use -

NSString *aContactfirstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
NSString *aContactLastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) autorelease];
rishi
  • 11,779
  • 4
  • 40
  • 59
0
if(aContactLastName != NULL){
              aContactName = aContactLastName;//aContactName pointing to aContactLastName
            }  

In this you are assigning aContactLastName to aContactName(aContactLastName and aContactName are pointing to same memory location). and after that you are releasing aContactLastName.

if (aContactLastName){
                CFRelease(aContactLastName);
              }

and then you are returning return aContactName; (aContactName is already released) this is wrong.

remove

if (aContactLastName){
                CFRelease(aContactLastName);
              }

from your code and return [aContactName autorelease];

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144