Skybird
Skybird

Reputation: 159

Xcode Static Analyser reports one leak under ARC - CFImageRef - how to resolve

Out of some 2k lines of code, the Static Analyser has only one problem, thus:

spellDetailModalViewController  *detailVC = [[spellDetailModalViewController alloc]init];
UIImage *tempImage = self.spellImageView.image;
CGRect newSize = CGRectMake(0.0, 0.0, 320.0, 305.0);
CGImageRef temp = CGImageCreateWithImageInRect([tempImage CGImage], newSize);
UIImage *passingImage = [UIImage imageWithCGImage:temp];
temp=nil;

It is complaining that CGImageRef 'temp' is 'potentially' being leaked, and has a retain count of +1. I set it to nil after the image has been passed to the modal ViewController. Obviously, under ARC, I can't call [temp release] under ARC. Not sure what to do. Help greatly appreciated.

Upvotes: 3

Views: 1387

Answers (2)

Kemal Can Kaynak
Kemal Can Kaynak

Reputation: 1658

CGImage is a Core Graphics object and ARC can not handle Core Libraries. So you should use CGImageRelease or better way to pass that warning, use imageWithCIImage method like that;

CIImage *fooImage = [CIImage imageWithCGImage:temp.CGImage];
UIImage *passingImage = [UIImage imageWithCIImage:fooImage];

Upvotes: 0

zaph
zaph

Reputation: 112855

You need to CGImageRelease temp

CGImageRef temp = CGImageCreateWithImageInRect([tempImage CGImage], newSize);
UIImage *passingImage = [UIImage imageWithCGImage:temp];
CGImageRelease(temp);

From the CGImageCreateWithImageInRect Apple docs:

The resulting image retains a reference to the original image, which means you may release the original image after calling this function.

Upvotes: 4

Related Questions