gpt4 book ai didi

ios - viewWillAppear 中的 initWithContentsOfFile Plist 内存泄漏

转载 作者:行者123 更新时间:2023-11-29 04:39:37 25 4
gpt4 key购买 nike

我正在 ViewVillAppear 中加载 plist 文件,如下所示。第一次加载时,我没有泄漏,但是按下其他 tabBar 按钮/项目并返回到此 View 时,我出现泄漏。我已经在 dealloc 中释放了这个 NSMutableArray 但它仍然泄漏。有点困惑为什么。(theProducts3 是一个 NSMutableArray,就像 .h 中的 ivar 一样,它不是 @property 或保留的)

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [rootPath stringByAppendingPathComponent:@"basket.plist"];
theProducts3 = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];

NSLog(@"Number of objects in item array %i", [theProducts3 count]);
}

在此处释放 NSMutable 数组。

-(void)dealloc{
[theProducts3 release];
[super dealloc];
}

非常感谢任何指点!谢谢...

最佳答案

dealloc不是 viewWillAppear: 的倒数.它是 alloc 的倒数. viewWillAppear: 的倒数是viewWillDisappear: .

发生的情况是,当您的 View 出现时,您正在分配内存,然后您将转到不同的 View Controller ,回来时,您的 View 再次出现,并且您正在分配更多内存,从而泄漏了原始内存内存。

如果您的数组只需在 View 位于内存中时就需要保留,则将其分配在 viewDidLoad: 中并在 viewDidUnload: 中发布和 dealloc .请记住将实例变量设置为 nil释放后。

关于ios - viewWillAppear 中的 initWithContentsOfFile Plist 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com