简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM