Mixstah
Mixstah

Reputation: 411

UIGraphicsContext memory leak

Hi In my app I have a function that takes an Image of the current view and turns it into a blurred image then adds it to the current.view. All though I remove the view using [remove from superview] it the memory still stays high. I am using core graphics and set all of the UI Images to zero.

I do get a memory leak warningenter image description here

-(void)burImage
{
    //Get a screen capture from the current view.
    UIGraphicsBeginImageContext(CGSizeMake(320, 450));
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the image
    CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];

    CGAffineTransform transform = CGAffineTransformIdentity;
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    [clampFilter setValue:blurImg forKey:@"inputImage"];
    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    [gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat:22.0f] forKey:@"inputRadius"];

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]];
    UIImage *outputImg = [UIImage imageWithCGImage:cgImg];

    //Add UIImageView to current view.
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 450)];
    [imgView setTag:1109];

    imgView.image = outputImg;
    [imgView setTag:1108];

    gaussianBlurFilter = nil;
    outputImg = nil;
    blurImg = nil;

    viewImg = nil;
    [self.view addSubview:imgView];
    UIGraphicsEndImageContext();
}

Upvotes: 1

Views: 2892

Answers (1)

Rob
Rob

Reputation: 437532

The static analyzer ("Analyze" on the Xcode "Product" menu) is informing you that you are missing a needed CGImageRelease(cgImg) at the end of your method. If you have a Core foundation object returned from a method/function with "Create" or "Copy" in the name, you are responsible for releasing it.

By the way, if you tap on the enter image description here icon (once in the margin, and again on the version that appears in the error message), it will show you more information:

enter image description here

That can be helpful for tracking back to where the problem originated, in this case the call to createCGImage. If you look at the documentation for createCGImage, it confirms this diagnosis, reporting:

Return Value

A Quartz 2D image. You are responsible for releasing the returned image when you no longer need it.

For general counsel about releasing Core Foundation objects, see the Create Rule in the Memory Management Programming Guide for Core Foundation.

Upvotes: 1

Related Questions