0

I am getting a issue with NSMutableArray. I have used that to store contact details form address book. I have 300 contacts in address book. But my app is going to crashes. I have used this code

+(NSMutableArray *)getcontactdetails
{
  ABAddressBookRef addressBook;
  CFArrayRef allSources;
  NSMutableArray *list = [[NSMutableArray alloc] init];
  addressBook = ABAddressBookCreate();
  ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);

  allSources = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonFirstNameProperty);
  CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

  if(nPeople != 0){
    for (CFIndex i = 0; i < nPeople; i++)
    {
        //common field
        NSString *first_name =[[[NSString alloc] init] autorelease];
        contact_details *phone_book_data=[[contact_details alloc] init];
        ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,i);
        CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
        first_name=[NSString stringWithFormat:@"%@",firstName];

        if ([first_name isEqualToString:@"(null)"] || first_name == nil || first_name.length == 0) {
        }
        else{
            phone_book_data.FirstName=[NSString stringWithFormat:@"%@",first_name];
        }
        [list addObject:phone_book_data];
        phone_book_data=nil;
        [phone_book_data release];

    }
    CFRelease(allSources);
  }

  [self current_function_name:@"Finished getcontactdetails"];

  return list;
 }

I have got these on console:

Sep 3 23:09:20 iPhone ReportCrash[1378] : Formulating crash report for process intooch[1373] Sep 3 23:09:21 iPhone com.apple.launchd[1] (UIKitApplication:com.inTooch.inTooch[0x6b8a][1373]) : (UIKitApplication:com.inTooch.inTooch[0x6b8a]) Job appears to have crashed: Segmentation fault: 11 Sep 3 23:09:21 iPhone SpringBoard[52] : Application 'inTooch' exited abnormally with signal 11: Segmentation fault: 11 Sep 3 23:09:21 iPhone ReportCrash[1378] : libMobileGestalt computeUniqueDeviceID: total time for bb to return imei: 0

How I fixed that issue?

Thanks in advance.....

Andriy
  • 2,767
  • 2
  • 21
  • 29
ios
  • 552
  • 5
  • 22
  • 3
    What is the point of (1) using `[NSString stringWithFormat:@"%@",firstName]` instead of `firstName` directly?!? (2) same for `phone_book_data.FirstName=[NSString stringWithFormat:@"%@",first_name];` (3) assigning `first_name` to an empty string as then you reaffect it with a different value a few lines later?! (4) Using `isEqualToString:@"(null)"` -- that is based on the description of the string -- instead of comparing the string with `NSNull`? (5) `CFRelease(allSources)` only when `nPeople != 0`, leaking otherwise? Seems a lot of basic memory management rules have to be studied again here ;) – AliSoftware Sep 04 '12 at 15:04
  • @AliSoftware thanks can u make some more help to fix this issue? – ios Sep 04 '12 at 15:14

1 Answers1

1

You are leaking memory here:

CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
first_name=[NSString stringWithFormat:@"%@",firstName];

Just get rid of the second line altogether, all it does is introduce a memory leak.

The release here is meaningless:

phone_book_data=nil;
[phone_book_data release];

Since you assign nil to the pointer first, you are leaking the memory of the object it is pointing to. Swap these lines around.

You also need to rename this method to indicate that the calling code owns the resulting object.

You really need to read up on memory management. Start by running the static analyser, it should flag up a number of issues with this code, and don't stop reading until you realise the cause of each warning.

Jim
  • 72,985
  • 14
  • 101
  • 108