Static Analyzer says I have a leak....why?

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.


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.

链接地址: http://www.djcxy.com/p/43734.html

上一篇: 潜在的内存泄漏使用分析

下一篇: 静态分析仪说我有泄漏....为什么?