Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 02-09-2010, 04:45 PM   #1 (permalink)
borg359
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default 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);

}
borg359 is offline   Reply With Quote
 

» Advertisements
» Online Users: 358
18 members and 340 guests
ADY, apatsufas, chemistry, Duncan C, Fit4him, headkaze, HemiMG, MarkC, marto1914, Meoz, padsoftware, Promo Dispenser, Raffaello, sojourner, tathaastu, tgjorgoski, Thrillhouse1919, ziocleto
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,877
Threads: 89,222
Posts: 380,718
Top Poster: BrianSlick (7,129)
Welcome to our newest member, peterkessler45
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.