views:

43

answers:

1

Hi guys, I getting leak here which is written in the appDelegate.m

-(NSMutableArray*)getSalutationList
{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    [list addObject:@"Dr."];
    [list addObject:@"Mr."];
    [list addObject:@"Mrs."];
    [list addObject:@"Miss."];
    [list addObject:@"Ms."];
    return list;    //return [list autorelease]; if i keep this  i am getting exception.
}

How to release the list and also i need the content which I am calling from view controller.

+3  A: 

You're getting a reported leak because the memory management guidelines say that you're supposed to be returning an autoreleased object. In addition, you shouldn't be prefixing your method name with "get" unless you're planning on providing data via an out parameter.

So your method should be:

-(NSMutableArray*) salutationList {
    NSMutableArray *list=[[NSMutableArray alloc]init];
    [list addObject:@"Dr."];
    [list addObject:@"Mr."];
    [list addObject:@"Mrs."];
    [list addObject:@"Miss."];
    [list addObject:@"Ms."];
    return [list autorelease];
}

If you're getting an exception by returning [list autorelease], then your problem lies elsewhere (perhaps you're releasing the array somewhere else when you shouldn't be?).

Dave DeLong
It is declared locally and not released any where. Is it possible to release the main list and return the temperory list
Madan Mohan