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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 04:45 PM   #1 (permalink)
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
Old 02-09-2010, 05:08 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,091
Default

This is a leak:

Code:
[tmpMarkers addObject:[item copy]];
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-09-2010, 05:43 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default Thanks, but why is this a leak?

Quote:
Originally Posted by BrianSlick View Post
This is a leak:

Code:
[tmpMarkers addObject:[item copy]];
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?
borg359 is offline   Reply With Quote
Old 02-09-2010, 05:45 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,091
Default

Code:
NSMutableDictionary *copyOfItem = [item copy];
[tmpMarkers addObject: copyOfItem];
[copyOfItem release], copyOfItem = nil;
It's not the copy in and of itself, it's how you were doing it.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, leaks, memory, nsmutabledictionary, sql

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,480
Threads: 89,092
Posts: 380,125
Top Poster: BrianSlick (7,091)
Welcome to our newest member, premkumarmsc1
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0