任何人都可以告诉我为什么以下代码泄漏?仪器告诉我大约2次泄漏。明显导致泄漏的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;
}
提前感谢您的帮助!
亲切的问候
菲尔
答案 0 :(得分:0)
假设setPerson调用保留在pers上。你的PersonViewController是否dealloc,在该person对象上调用release?如果是这样,在那里放置一个断点(或NSLog)并找出该人的retainCount。如果不是0,你还可以保留它吗?
答案 1 :(得分:0)
谢谢你们的回复。 PersonViewController确实保留了person对象,但我在dealloc中为person对象设置了一个release。零售额还可以。 我将Person对象的初始化移动到PersonViewController,现在一切都很好。这对我来说似乎很奇怪。
谢谢
此致