0

Can anyone please tell me why the following code leaks? Instruments tells me about 2 leaks. The 2 lines that obviously cause the leak are:

Person *pers = [[Person alloc] init];

and

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

The whole is listed below:

PersonViewController *personenDatenController = [[PersonViewController alloc]
          initWithStyle:UITableViewStyleGrouped];

personenDatenController.view.backgroundColor = [UIColor clearColor];

 Person *pers = [[Person alloc] init];

 NSString *path = [[self class] pathForDocumentWithName:@"Person.plist"];
 BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
 if (!fileExists) {
  NSLog(@"file does not exist yet");
  NSString *content = @"";
  NSData *fileContents = [content dataUsingEncoding:NSUTF8StringEncoding];
  [[NSFileManager defaultManager] createFileAtPath:path
            contents:fileContents
             attributes:nil];

 }

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] 
         initWithContentsOfFile:path];
 [pers setVorName:[dict valueForKey:@"vorName"]];
 [pers setNachName:[dict valueForKey:@"nachName"]];
 [pers setStrassenName:[dict valueForKey:@"strassenName"]];
 [pers setHausNummer:[dict valueForKey:@"hausNummer"]];
 [pers setPlz:[dict valueForKey:@"plz"]];
 [pers setStadt:[dict valueForKey:@"stadt"]];
 [pers setHandyNummer:(NSInteger*)[dict valueForKey:@"handyNummer"]];
 [pers setEmail:[dict valueForKey:@"email"]];
 [pers setSteuerSatz:[[dict valueForKey:@"steuerSatz"] floatValue]];
 [dict release];


    [personenDatenController setPerson:pers];

    [navigationController pushViewController:personenDatenController animated:YES];

 [personenDatenController release];

    [pers release];

The variable "path" comes from the following static method:

+ (NSString *)pathForDocumentWithName:(NSString *)documentName
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *tempPath = [documentsDirectory stringByAppendingPathComponent:documentName];

    return tempPath;
}

Thanks in advance for your help!

Kind regards

Phil

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Looks ok. Maybe let the static analyzer have a look in it. Keep in mind that the person object *should* be around as long as the personenDataController is visible - which might be reported wrongly as a leak if your snapshots are timed badly. Lastly, maybe this PersonViewController leaks those objects. – Eiko Aug 18 '10 at 11:52

2 Answers2

0

Assuming that setPerson calls retain on pers. Does your PersonViewController dealloc, call release on that person object? If so, put a breakpoint there (or NSLog) and find out the retainCount of the person. If it's not going to 0, where else might you have retained it?

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

Thank you guys for your responses. PersonViewController does retain the person object but I put a release for the person object in dealloc. The retaincount is okay. I moved the initialization of the Person object to the PersonViewController and now everything is fine. This seems quite strange to me.

Thank you

Regards