 |
 |
|
 |
02-09-2010, 03:45 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Posts: 15
|
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);
}
|
|
|
02-09-2010, 04:08 PM
|
#2 (permalink)
|
|
Emphasizing Fundamentals
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,979
|
This is a leak:
Code:
[tmpMarkers addObject:[item copy]];
|
|
|
02-09-2010, 04:43 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Posts: 15
|
Thanks, but why is this a leak?
Quote:
Originally Posted by BrianSlick
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?
|
|
|
02-09-2010, 04:45 PM
|
#4 (permalink)
|
|
Emphasizing Fundamentals
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,979
|
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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 319 |
| 24 members and 295 guests |
| abidins, alexanderlonsky, bluelobster, catoguchi, damienbern, darth_steff, Erle, Falcon80, HemiMG, iphonedave, iTrackiGiveaway, jamison, javaconvert, LemonMeringue, linkmx, Noise, phenolic, RedM, robertyang999, schoash, scotopia, ShayansMind, StefanL, ToM |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,261
Threads: 39,058
Posts: 171,278
Top Poster: smasher (2,575)
|
| Welcome to our newest member, SomeRandomNerd |
|