Instruments Leaks 讲述了初始化对象时的内存泄漏
谁能告诉我为什么以下代码会泄漏? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢你们的回复。
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
假设 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?