gpt4 book ai didi

ios - NSDictionary initWithContentsOfFile 内存泄漏问题

转载 作者:行者123 更新时间:2023-11-29 10:58:41 26 4
gpt4 key购买 nike

我正在使用静态分析器检查我的代码的内存泄漏,我发现以下部分有潜在的泄漏。

NSString *path = nil;
NSString *tutorialPath = nil;
if (CC_CONTENT_SCALE_FACTOR() == 2)
{
path = [[NSBundle mainBundle] pathForResource:@"sheetObjects-hd" ofType:@"plist"];
tutorialPath = [[NSBundle mainBundle] pathForResource:@"sheetTutorial-hd" ofType:@"plist"];
} else
{
path = [[NSBundle mainBundle] pathForResource:@"sheetObjects" ofType:@"plist"];
tutorialPath = [[NSBundle mainBundle] pathForResource:@"sheetTutorial" ofType:@"plist"];
}

_animDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] objectForKey:@"frames"];
_tutorialAnimDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:tutorialPath] objectForKey:@"frames"];

问题出在这两行:

_animDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] objectForKey:@"frames"];
_tutorialAnimDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:tutorialPath] objectForKey:@"frames"];

我检查了我的 dealloc 代码,我很确定它们被正确地释放了。

这就是我定义实例的方式:

NSDictionary *_animDataDictionary;
NSDictionary *_tutorialAnimDataDictionary;

释放函数:

[_animDataDictionary release];
_animDataDictionary = nil;
[_tutorialAnimDataDictionary release];
_tutorialAnimDataDictionary = nil;
[super dealloc];

通过检查其他相关问题,我看到有人提示类似的错误,但没有人真正得到答案并知道为什么会发生。

我有大量与此代码相关的漏洞,我觉得有必要终止它。

谢谢!

最佳答案

在我看来,您正在泄露 NSDictionary 对象,正如静态分析器所指出的那样。您没有将 [[NSDictionary alloc] initWithContentsOfFile:path][[NSDictionary alloc] initWithContentsOfFile:tutorialPath] 的结果存储在任何地方,因此您无法发送这些结果对象显式释放消息。

尝试在创建这些中间字典后添加自动释放调用,例如:

_animDataDictionary = [[[[NSDictionary alloc] initWithContentsOfFile:path] autorelease] objectForKey:@"frames"];
_tutorialAnimDataDictionary = [[[[NSDictionary alloc] initWithContentsOfFile:tutorialPath] autorelease] objectForKey:@"frames"];

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

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