iphone - App crashes dur to NSMutable Array

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.....

1 Answer

  1. Luke- Reply

    2019-11-14

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>