Memory leak when filtering with Core image
Asked Answered
K

1

3

So I've been using core image to apply filters on images, everything is good except when I try to apply the same filter over and over again the application just quits, I guess its a memory leak.

Here's the code:

-(UIImage *) applyFilter: (UIImage*) picture
{

    UIImageOrientation originalOrientation = picture.imageOrientation;
    CGFloat originalScale = picture.scale;   


    CIImage *beginImage = [CIImage imageWithCGImage:picture.CGImage];  


    CIContext *context = [CIContext contextWithOptions:nil];

    CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" 
                                  keysAndValues: kCIInputImageKey, beginImage, 
                        @"inputIntensity", [NSNumber numberWithFloat:0.8], nil];

    CIImage *outputImage = [filter outputImage];

    CGImageRef cgimg = 
    [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg scale:originalScale orientation:originalOrientation];

    beginImage = nil;
    context = nil;
    filter = nil;
    outputImage = nil;
    cgimg = nil;
    [beginImage release];
    [context release];
    [filter release];
    [outputImage release];
    //CGImageRelease(CGImageRef) method.
    CGImageRelease(cgimg);

    return newImg;
}

And to filter I simply do

UIImage *ima = [self.filter applyFilter:self.imageView.image];
imageView.image = ima ;

The applyFilter is a method of Filter class that I created

Kremlin answered 27/6, 2012 at 9:6 Comment(0)
E
3

You set variables to nil before you call release, so the release has no effect. But you should not release most of the stuff anyway. You only need to release objects that you created (I hope the following list is complete):

  • Objective-C objects that were returned by methods starting with alloc, init, copy, new
  • Foundation objects returned by Objective-C methods starting with create, or by functions containing Create or Copy.

Delete these lines and it should be fine:

beginImage = nil;
context = nil;
filter = nil;
outputImage = nil;
cgimg = nil;
[beginImage release];
[context release];
[filter release];
[outputImage release];

You need to keep the line CGImageRelease(cgimg); because the method used to get cgimg contains create – you create it, you release it.

Epigastrium answered 27/6, 2012 at 9:34 Comment(3)
Hi @Meenakshi, should you share the code that hasn't worked for you? Do you use ARC?Epigastrium
Hi, Yes I am using ARC and I have added the question, link is https://mcmap.net/q/1482231/-memory-leak-for-coreimage Here I have added the code.Hawes
@Tammo Freese Can you please review code and plz tell me what should i done wrong in code?Hawes

© 2022 - 2024 — McMap. All rights reserved.