Meenakshi
Meenakshi

Reputation: 359

Memory leak for CoreImage

I have created blur image as below:

//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 2] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
CIImage *finalImage = [resultImage imageByCroppingToRect:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height)];

//create UIImage from filtered image
UIImage *blurrredImage = [[UIImage alloc] initWithCIImage:finalImage];

CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"];
[colorMatrixFilter setDefaults];
[colorMatrixFilter setValue:finalImage forKey:kCIInputImageKey];
[colorMatrixFilter setValue:[CIVector vectorWithX:0.25 Y:0 Z:0 W:0] forKey:@"inputRVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0.35 Z:0 W:0] forKey:@"inputGVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0.4 W:0] forKey:@"inputBVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"];

// Get the output image recipe
CIImage *outputImage = [colorMatrixFilter outputImage];

// Create the context and instruct CoreImage to draw the output image recipe into a CGImage
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
blurrredImage = [UIImage imageWithCGImage:cgimg];


CGImageRelease(cgimg);

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = blurrredImage;

//insert blur UIImageView below transparent view inside the blur image container
[blurContainerView insertSubview:newView belowSubview:transparentView];

I have released the CGImageRef, but still getting the memory leak at

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

I am also referred all other related questions and and implement the provided answers still getting memory leak

Used @autoreleasepool { } and try to set object to nil

context = nil;
outputImage = nil;
gaussianBlurFilter = nil;
viewImage = nil;

before

CGImageRelease(cgimg);

Still it shows the memory leak and description is as below. ARC is on and ios version is 6.1 and 7

Responsible Library is : CoreImage Responsible Caller is : CI::GLESContext::program_for_name(__CFString const*)

Can anybody please suggest me how can i resolved this memory leak?

Thanks for the help in advance.

Upvotes: 0

Views: 2933

Answers (2)

Jeffrey Monte
Jeffrey Monte

Reputation: 700

The problem would be the constant call

[CIContext contextWithOptions:nil]

I would recommend creating a property of CIContext in your class:

@property (retain, nonatomic) CIContext *context;

and only instantiate it when your context is nil

if( self.context == nil ) {
    self.context = [CIContext contextWithOptions:nil];
}

Upvotes: 1

dandan78
dandan78

Reputation: 13854

All of the Core frameworks use the C language, not Objective-C. You have to do memory management manually and ARC won't help you very much there except when transferring ownership of some (toll-free bridged) references from Core to Objective-C objects. You really should read the Apple documentation to understand what you need to do.

In short, though, setting a references to nil (should be NULL in C) leaves you with a dangling pointer and a memory leak.

Upvotes: 1

Related Questions