NSMutableArray分配中的内存泄漏

时间:2010-03-23 13:01:04

标签: objective-c memory-leaks nsmutablearray

我在

中的NSMutableArray分配中遇到内存泄漏
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;

}

2 个答案:

答案 0 :(得分:2)

从方法返回的对象(在您的情况下,contactsArray)是返回之前自动释放的标准过程。 您可以return [contactsArray autorelease];创建它,也可以使用[NSMutableArray arrayWithCapacity:noOfPeople]

自动释放它

答案 1 :(得分:0)

您需要在某处手动释放contactsArray,因为它没有定义自动释放。