1

So I have been scratching my head of the past few days trying to patch a memory leak while trying to blur an image using the core image. I have traced the memory leakage down to this block of code:

- (void) blurImage {
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"Captured Image");

    @autoreleasepool {
        CIImage *inputImage = [[CIImage alloc] initWithCGImage:image.CGImage];
        CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [filter setValue:inputImage forKey:@"inputImage"];
        [filter setValue:[NSNumber numberWithFloat:10.0] forKey:@"inputRadius"];

        CIImage *result = [filter valueForKey:kCIOutputImageKey];
        blur_image.image= [[UIImage alloc] initWithCIImage:result];
    }
    NSLog(@"blurred Image");
}

This code does two things, it first captures a screen shot of the screen at the time, then it blurs that image and sets it int to an image view in the background of my superview.

I am calling this block of code using a selector to run in the background

- (void) viewDidAppear:(BOOL)animated{
    SEL blur = @selector(blurImage);
    [self performSelectorInBackground:blur withObject:nil];
}

I am getting a 3 memory leaks that is associated with this code

This is the leakage I mentioned: CoreImage CI::GLESContext::program_for_name(__CFString const*)

I am befuddled as to what i am doing wrong can someone enlighten me?

Adithya
  • 63
  • 1
  • 8
  • `performSelectorInBackground:withObject:` is somewhat notorious for making it easy to leak. This method creates a new thread and runs the selector on that thread. Some of the UIKit functionality you are using is not safe to use from any thread but the main thread, which is not good. However, to find your leak, post some information from Instruments such as a screenshot of a trace using the Leaks instrument. – quellish Aug 31 '14 at 00:52
  • lol, i don't have enough reputation i will post a text version though – Adithya Aug 31 '14 at 02:03

1 Answers1

1

Few points here

Community
  • 1
  • 1
tia
  • 9,518
  • 1
  • 30
  • 44
  • I've decided to use apple's uiimageview+effects header but I still want to understand what i did wrong here... I don't see what would be causing a memory leakage. I've enven tried CGImageReleasing(image.CGImage); the image assignment. – Adithya Aug 31 '14 at 14:33
  • For example, the `image` is leaked because it runs with no autorelease pool. – tia Aug 31 '14 at 14:43
  • Why would it though, I thought ARC manages all Objective-C objects. – Adithya Aug 31 '14 at 14:53
  • ARC does not manage objects, it just manage retain/release/autorelease and it needs autorelease pool to work correctly. If you see your `main.m` file, you will see `@autoreleasepool` there, because it is not something that ARC provides. – tia Aug 31 '14 at 16:01