静态分析器说我有泄漏......为什么?

发布于 2024-10-11 16:09:20 字数 538 浏览 2 评论 0原文

我认为这段代码应该没问题,但静态分析器不喜欢它。我不明白为什么,希望有人能帮助我理解。代码工作正常,分析器结果只是让我烦恼。

Coin *tempCoin = [[Coin alloc] initalize];
self.myCoin = tempCoin;
[tempCoin release];

Coin 是一个通用的 NSObject 并且它有一个 initalize 方法。 myCoin 是当前视图的属性,类型为 Coin。我认为它告诉我我正在泄漏 tempCoin

在我看来的 .h 中,我已将 myCoin 设置为非原子、保留的属性。

我尝试自动释放代码以及正常发布,但静态分析器继续显示:
1. 方法返回一个 Objective-C 对象,其保留计数+1(拥有引用)
2. 第 97 行分配的对象在此之后不再被引用,并且保留计数为 +1(对象泄漏)

第 97 行是我显示的第一行。

I think this code should be fine but Static Analyzer doesn't like it. I can't figure out why and was hoping that someone could help me understand. The code works fine, the analyzer result just bugs me.

Coin *tempCoin = [[Coin alloc] initalize];
self.myCoin = tempCoin;
[tempCoin release];

Coin is a generic NSObject and it has an initalize method. myCoin is a property of the current view and is of type Coin. I assume it is telling me I am leaking tempCoin.

In my view's .h I have set myCoin as a property with nonatomic,retain.

I've tried to autorelease the code as well as this normal release but Static Analyzer continues to say:
1. Method returns an Objective-C object with a +1 retain count (owning reference)
2. Object allocated on line 97 is no longer referenced after this point and has a retain count of +1 (object leaked)

Line 97 is the first line that I show.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-10-18 16:09:20

因为静态分析器正在寻找 init,而不是 initialize。它看到后者并假设 [Coin alloc] 返回的对象返回与 initialize 不同的对象,从而泄漏了第一个对象。

将方法名称更改为init,静态分析器将不再报告泄漏。

Because the static analyzer is looking for init, not initialize. It sees the latter and assumes that the object returned by [Coin alloc] returns a different object from initialize, thus leaking the first object.

Change the name of the method to init and the static analyzer will no longer report a leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文