Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top