静态分析器说我有泄漏......为什么?
我认为这段代码应该没问题,但静态分析器不喜欢它。我不明白为什么,希望有人能帮助我理解。代码工作正常,分析器结果只是让我烦恼。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为静态分析器正在寻找
init
,而不是initialize
。它看到后者并假设[Coin alloc]
返回的对象返回与initialize
不同的对象,从而泄漏了第一个对象。将方法名称更改为
init
,静态分析器将不再报告泄漏。Because the static analyzer is looking for
init
, notinitialize
. It sees the latter and assumes that the object returned by[Coin alloc]
returns a different object frominitialize
, thus leaking the first object.Change the name of the method to
init
and the static analyzer will no longer report a leak.