Question

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.

Was it helpful?

Solution

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.

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