views:

67

answers:

2

Hi @ all

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

A: 

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
A: 

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

Philipp Noggler