CGImageCreateWithImageInRect Static Analyze Warning

I have the following function part of a UIImage Category:

- (UIImage *)copyImageAtRect:(CGRect)rect {
CGImageRef imageToSplit = self.CGImage;
CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToSplit, rect);
UIImage *image = [UIImage imageWithCGImage:partOfImageAsCG];
CGImageRelease(partOfImageAsCG);
return image;}

However, when I analyse my code I get the following static analyze warning: "Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected" with the return image line highlighted.

I've tried changing many things but can't seem to work out why this is coming out as a potential leak.

Any help is greatly appreciated.

Many thanks.

EDIT As waldrumpus pointed out, the problem was the name of my function. It includes the word "copy" which the compiler was picking up on. Fixed this by renaming the function. Thank you waldrumpus.


Because your method's name begins with the word copy... , the compiler assumes by convention that it will return a retained copy of the object. This pertains to method names beginning with copy... or init... .

Rename the method to something else, and the warning should disappear.

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

上一篇: 为什么静态分析器在此代码中警告垃圾值?

下一篇: CGImageCreateWithImageInRect静态分析警告