Instruments Leaks 讲述了初始化对象时的内存泄漏

发布于 2024-09-15 06:14:57 字数 2180 浏览 6 评论 0原文

谁能告诉我为什么以下代码会泄漏? Instruments 告诉我有 2 处泄漏。明显导致泄漏的两行是:

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

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

整体如下:

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];

变量“path”来自以下静态方法:

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

    return tempPath;
}

提前感谢您的帮助!

亲切的问候

菲尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-09-22 06:15:22

谢谢你们的回复。
PersonViewController 确实保留了 person 对象,但我在 dealloc 中释放了 person 对象。保留计数没问题。
我将 Person 对象的初始化移至 PersonViewController 中,现在一切都很好。这对我来说似乎很奇怪。

谢谢您的

问候

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

叶落知秋 2024-09-22 06:15:18

假设 setPerson 调用保留在 pers 上。您的 PersonViewController 是否对该 person 对象进行释放、调用释放?如果是这样,在那里放置一个断点(或 NSLog)并找出该人的保留计数。如果它不会变为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文