开发者

Memory leak in NSMutableArray allocation

开发者 https://www.devze.com 2022-12-24 14:31 出处:网络
I am getting memory leak in NSMutableArray allocation.. in NSMutableArray *contactsArray =[[NSMutableArray alloc] init];

I am getting memory leak in NSMutableArray 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);
        NS开发者_StackOverflowString* personIdStr = [NSString stringWithFormat:@"%d", personId];

        ContactDTO* contactDTO = [AddressBookUtil getContactDTOForId:personIdStr];


        [contactsArray addObject:contactDTO];

    }
    [peopleArray release];
    return contactsArray;

}


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]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号