Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potential leak of an object stored into CGImageRef

Running my code under Xcode Analyze i’ve stumbled into the following block

- (UIImage *)imageWithFilter:(CIFilter *)filter
{
    CIContext *ctx = [CIContext contextWithOptions:nil];
    CGImageRef imageRef = [ctx createCGImage:filter.outputImage fromRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    return [UIImage imageWithCGImage:imageRef];
}

Xcode complains about a potential memory leak:

enter image description here

What is going on? And how would I go about fixing it?

like image 284
Maxim Veksler Avatar asked Jun 20 '14 11:06

Maxim Veksler


1 Answers

The following looks like a fix, still not sure if this is the best way of handling this over retained reference?

- (UIImage *)imageWithFilter:(CIFilter *)filter
{
    CIContext *ctx = [CIContext contextWithOptions:nil];
    CGImageRef imageRef = [ctx createCGImage:filter.outputImage fromRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return image;
}
like image 107
Maxim Veksler Avatar answered Oct 06 '22 02:10

Maxim Veksler