views:

51

answers:

3

i been cracking my head over this memory leak..

my datasource is mutabledictionary..that i load in the viewdidload. if i dont retain it. i dont have access it it in cellforrowatindexpath. but when i retain it.. it shows up as a memory leak in instruments. i have tried so many different variations.. doesnt seem to get it right.

here is the code the leak is in "dict" and "plistPath"

` - (void)viewDidLoad { [super viewDidLoad];

self.navigationController.navigationBarHidden = NO;

self.title = @"Messages & Lists";


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

[plistPath release];
plistPath = [documentsDirectory stringByAppendingPathComponent:@"general.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];
[dict release];

if ( [fileManager fileExistsAtPath:plistPath] ) {

    dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath] ;


} else {
    dict = [NSMutableDictionary dictionaryWithCapacity:1];

    [dict setObject:@"NO" forKey:@"busyStatus"];
    [dict setObject:@"NO" forKey:@"replyToAll"];
    [dict setObject:@"NO" forKey:@"replyToList"];
    [dict setObject:@"NO" forKey:@"dontReplyToList"];

    [dict writeToFile:plistPath atomically:YES];


}

[tableData release];

tableData = [[NSMutableDictionary alloc] init];

[tableData setObject:[NSArray arrayWithObjects:@"Help",@"Set Default Message",@"Reply To All",[dict objectForKey:@"replyToAll"],nil] forKey:@"1"];
[tableData setObject:[NSArray arrayWithObjects:@"Reply to a List",[dict objectForKey:@"replyToList"],@"List of Contacts",nil] forKey:@"2"];
[tableData setObject:[NSArray arrayWithObjects:@"Don't reply to List",[dict objectForKey:@"dontReplyToList"],@"List of Contacts",nil] forKey:@"3"];


[dict retain];  
[plistPath retain];

}

`

there is no leak the first time the view loads. but if i got back. and then load the view again it leaks.

thanks in advance for anyone who can help me out.

+2  A: 

You have to call [dict release] in your view controller's dealloc method.

Jason Coco
ok thanks. that seem to have solved the problem. i dont knw hw i missed it.
Jinah Adam
Rule of the thumb: All properties (for objects) set in .h should be released in the dealloc-method. And the NARC- New Alloc, Retain, Copy - if you do one of these, be sure to release the object later.
Emil
A: 

I think you should release your dictionary in the dealloc method of your view controller so that the retain count of your datasource is decreased when the ViewController is deallocated.

Retaining without releasing after use is the main source of memory leak.

David
A: 

It seems right. The only thing that confuses me is the tableData. Is that var released or allocated somewhere else too?

Say you do allocate it somewhere else, and not releases it in dealloc, shouldn't these be the steps then:

  1. tableData is allocated somewhere else.
  2. tableData is released in viewDidLoad
  3. tableData is allocated again (counter 1).
  4. Exiting view, entering view again, tableData allocated again, released and allocated in function (counter two?)

Seems like a longshot, but. Can you display what the instruments say?

Fredrik_jakob