Memory leaks when adding objects to NSMutableDictionary
Hi guys,
I have a pressing memory management question that I was hopping you guys could answer. Basically, I have a simple function that gathers the output from an SQL request and puts all the relevant data into an NSMutableArray for later use. When the user scrolls to a new area on a UIMapView, the NSMutableArray is released and a new one is created in it's place. Unfortunately, this simple model causes massive amounts of leaks (according to Apple's performance tool) which, because this function is called quite often, quickly grinds the program to a halt.
Apparently this happens when I assign a NSString to the item dictionary below, but I haven't been able to isolate why this is happening.
Anyway, here's the offending code. Any suggestions would be greatly appreciated.
Code:
// Defining the SQL callback functions
static int UnifiedCallback(void *context, int count, char **values, char **columns)
{
// Create a dictionary object and a mutable array to store the dictionaries
NSMutableArray * tmpMarkers = (NSMutableArray *)context;
NSMutableDictionary * item = [[NSMutableDictionary alloc] init];
int j = 0;
for (int i=0; i < count; i++) {
// Assign the output of the SQL
const char * nameCString = values[i];
if (j == 0) {
// Place the value of the first SQL column into a dictionary item with appropriate key
NSString * tmpName = [[NSString alloc] initWithUTF8String:nameCString];
[item setObject:tmpName forKey:@"Name"];
[tmpName release];
}
if (j == 1) {
// Place the value of the second SQL column into a dictionary item with appropriate key
NSString * tmpLocation = [[NSString alloc] initWithUTF8String:nameCString];
[item setObject:tmpLocation forKey:LocationKey];
[tmpLocation release];
}
if (j == 2) {
// Add the dictionary object to the mutable array and reset the item
[tmpMarkers addObject:[item copy]];
[item removeAllObjects];
j = 0;
} else {
j++;
}
}
[item release];
return SQLITE_OK;
}
Code:
- (void)viewWillAppear:(BOOL)animated {
[self loadNamesFromDatabase];
}
// Get the names and locations for all markers in the map view from the database
- (void)loadNamesFromDatabase {
// Release the Markers array if it already exists
if (Markers) { [Markers release]; }
// Create the database file name
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString * documentsDir = [paths objectAtIndex:0];
NSString * file = [documentsDir stringByAppendingPathComponent:@"Markers.db"];
Sprintf (unifiedCommand, blah blah);
sqlite3_exec(database, unifiedCommand, UnifiedCallback, Markers, NULL);
}
Thanks for point this out Brian. Without using a copy though, how would I prevent the dictionary item from disappearing from the NSMutableArray when I later release the item? The way I have it setup now, I reuse the dictionary item to fill the mutable array inside a for loop, so without the copy I would end up placing the same object in the array n number of times. Is there a proper way doing this without issuing a copy?