Memory leak for CoreImage
Asked Answered
F

2

0

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.

Fluxmeter answered 13/5, 2014 at 10:29 Comment(7)
Do you get other leaked objects as well, or only the CGImage object?Velvavelvet
get memory leaks for CoreImage and in main.m @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([OpinoAppDelegate class])); }Fluxmeter
What types are the other leaked objects? UIImage? UIImageView? Or are only CGImageRef objects leaked?Velvavelvet
Only CGImageRef objects are leaked May be its because of Context ObjectFluxmeter
That's a bit strange. The only CGImageRef you create you seem to release correctly. The CIContext is an NSObject and should be handled by ARC.Velvavelvet
I got the leak at CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; lineFluxmeter
Yes, leaks are always reported at the point the object was created. But it should not be reported as you release the object correctly. :(Velvavelvet
S
1

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.

Schulze answered 13/5, 2014 at 10:35 Comment(1)
Hi, thanks for the quick reply, I will check the documentation.Fluxmeter
A
1

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];
}
Accused answered 18/8, 2014 at 11:20 Comment(1)
I'm not sure that this is the solution. I have the leak even using a property. I think the general answer is to use @autoreleasepool, CG object Release, etc.Antennule

© 2022 - 2024 — McMap. All rights reserved.