gpt4 book ai didi

objective-c - 分析器声称某个对象在未释放时已被释放

转载 作者:行者123 更新时间:2023-11-28 22:50:59 26 4
gpt4 key购买 nike

我在这段代码中遇到了一个静态分析错误,这对我来说没有任何意义。错误是:

Reference-counted object is used after it is released

这是允许在最初用 C++ 编写的游戏中加载 PNG 的粘合代码。

int pngLoad(const char *filename, pngInfo *info, int format, GLuint *textureName)
{
char fullPath[Engine::Settings::MaxPath];
strcpy(fullPath, filename);
appendAppBundlePath(fullPath);

NSString *path = [NSString stringWithCString:fullPath encoding:NSUTF8StringEncoding];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
[data release];

Texture2D *loadedTex = [Texture2D alloc];

// ##### Analyzer claims the object is released here: #####
[loadedTex initWithImage:image format:format];

int didLoad;

// ##### Error is here: #####
if (loadedTex.contentSize.width == 0 || loadedTex.contentSize.height == 0)
{
didLoad = 0;
}
else
{
didLoad = 1;

*textureName = loadedTex.name;

// return texture info
info->ScaleFactor = loadedTex.scaleFactor;
info->Width = (float)image.size.width / (float)info->ScaleFactor;
info->Height = (float)image.size.height / (float)info->ScaleFactor;
info->Alpha = 1;
info->PaddedWidth = loadedTex.pixelsWide;
info->PaddedHeight = loadedTex.pixelsHigh;
}

[loadedTex release];
[image release];

return didLoad;
}

如果我使用 Texture2D *loadedTex = [[Texture2D alloc] retain]; 这个警告被删除了,但是随后出现了一个警告,说我已经泄漏了一个对象,所以这里有些东西很奇怪.

initWithImage:format: 曾经包含一个 [self release] ,它不应该存在,当我发现这个警告时我删除了它。然而,即使在完全清理和重建之后,我仍然收到警告。我做错了什么吗? Xcode 中的 Clean 命令是否没有正确清理某些内容?

最佳答案

分析者可能是对的,至少在一般意义上是这样。

Texture2D *loadedTex = [Texture2D alloc];
[loadedTex initWithImage:image format:format];

一般来说,“init”实际上可能会丢弃传入的对象并返回一个不同的对象。我不知道“Texture2D”是否属于这种情况,但如果分析器适用于一般情况,那么它是正确的。

你应该能够通过使用来解决这个问题

Texture2D *loadedTex = [Texture2D alloc];
loadedTex=[loadedTex initWithImage:image format:format];

或者像大多数 Objective-C 示例中所做的那样,简单地组合这两个调用。

关于objective-c - 分析器声称某个对象在未释放时已被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12018714/

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