views:

74

answers:

2

Hi
I am getting memory leak in mutablearray allocation.. in

NSMutableArray *contactsArray =[[NSMutableArray alloc] init]; 

CODE:

+(NSMutableArray*)getContacts
{
    addressBook = ABAddressBookCreate();

    NSArray* peopleArray =  (NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);

    int noOfPeople = [peopleArray count];

    NSMutableArray *contactsArray =[[NSMutableArray alloc] init]; 

    for ( int i = 0; i < noOfPeople; i++)
    {
        ABRecordRef person =   [peopleArray objectAtIndex:i];
        ABRecordID personId = ABRecordGetRecordID(person);
        NSString* personIdStr = [NSString stringWithFormat:@"%d", personId];

        ContactDTO* contactDTO = [AddressBookUtil getContactDTOForId:personIdStr];


        [contactsArray addObject:contactDTO];

    }
    [peopleArray release];
    return contactsArray;

}
A: 

You need to release contactsArray manually somewhere, because it does not define autorelease.

Tuomas Pelkonen
I tried using autorelease at the allocation. but it is causing exception.I need to release the array with in that method. Can u suggest any other solution
Srilakshmi Manthena
+2  A: 

It is standard procedure that objects returned from methods (in your case, contactsArray) are autoreleased before returning. You could either return [contactsArray autorelease]; or create it already autoreleased with [NSMutableArray arrayWithCapacity:noOfPeople]

Zydeco