I have my function getAllData, which is returning an array with dictionaries.
- (NSArray *)getAllData {
NSMutableArray *result = [[NSMutableArray alloc] init];
NSArray *data = [skiResorts sortedArrayUsingFunction:comparator context:NULL];
NSString *currentLetter = @"A";
NSMutableArray *array = [[NSMutableArray alloc] init] ;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init] ;
if ([data count] > 0) {
for (SkiResort *resort in data) {
if ([resort.name hasPrefix:currentLetter]) {
// Same letter as before.
// Add current SkiResort to temporary array.
[array addObject:resort];
} else {
// New letter.
// Add previous header/row data to dictionary.
[dict setValue:currentLetter forKey:@"header"];
[dict setValue:array forKey:@"row"];
// Add dictionary to final result array.
[result addObject:dict];
// Startover ...
[array removeAllObjects];
[dict removeAllObjects];
// Prepare for next letter.
currentLetter = [resort.name substringToIndex:1];
// Add current SkiResort to temporary array.
[array addObject:resort];
}
}
// Add previous header/row data to dictionary.
[dict setValue:currentLetter forKey:@"header"];
[dict setValue:array forKey:@"row"];
// Add dictionary to final result array.
[result addObject:dict];
}
[array release];
[dict release];
return [result autorelease];
}
Can anyone see obvious memory leaks in my code? I get memory leak array, dict, and result…
From the code, I have to ask: you’re aware that addObject: doesn’t copy the object? So setting values to dict, then adding it to result, then removing everything from dict just leaves an empty dictionary in result? I think you probably want to use the ‘copy’ method in there, to make copies of the array and dictionary. Or, even better, just create the dictionary when you add it to result using one of the class methods.
Anyway, since I can’t see any leaks in that, much more likely is that whoever receives the result of getAllData subsequently leaks it. If for some crazy reason you had somewhere a stray:
Then the leaks tool would identify a leak of array, dict and result and point you to getAllData as the method in which they were originally created.