tags:

views:

96

answers:

2

How do I fix the leak here?

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {   
if(searching){      
    return nil;
}

NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:UITableViewIndexSearch];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"E"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"I"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"N"];
[tempArray addObject:@"O"];
[tempArray addObject:@"P"];
[tempArray addObject:@"Q"];
[tempArray addObject:@"R"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
[tempArray addObject:@"W"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Y"];
[tempArray addObject:@"Z"];

return tempArray;
}

Any help would be appreciated.

Sam

+5  A: 

You should be returning an autoreleased object:

return [tempArray autorelease];
Felix
cheers, that fixed that specific problem.. now to fix the others. *sigh*
Sam Jarman
A: 

When you get the temparray, release it when done by calling

[#<your var># release];

to solve your leak. Autorelease will work, but you will need to set a NSAutoRelease pool and drain it after you're done to prevent a de facto leak (as the only autorelease pool is in main() at the beginning, so the program will not release until the program quits anyway).

futureelite7
The main() autorelease pool is drain at each run loop, not at the end of the program.
gcamp
yes, auto release is drained on the run loop. (or when the program needs it i believe)
Sam Jarman