zan
zan

Reputation: 21

Memory management issue with CIImage / CGImageRef

Good morning,

I encounter a memory management issue in the video processing software i'm trying to write. (video capture + (almost)real-time processing + display + recording).

The following code is part of the "..didOutputSampleBuffer.." function of AVCaptureVideoDataOutputSampleBufferDelegate.

capturePreviewLayer is a CALayer. ctx is a CIContext I reuse over and over. outImage is a vImage_Buffer.

With the commented section kept commented, memory usage is stable and acceptable, but if I uncomment it, memory won't stop increasing. Note that if I leave the filtering operation commented and only keep CIImage creation and conversion back to CGImageRef, the problem remains. (I mean : I don't think this is related to the filter itself).

If I run the XCode's Analyse, it points a potential memory leak if this part is uncommented, but none if it is commented.

Does anybody has an idea to explain and fix this ? Thank you very much !

Note : I prefer not to use AVCaptureVideoPreviewLayer and its filters property.

CGImageRef convertedImage = vImageCreateCGImageFromBuffer(&outImage, &outputFormat, NULL, NULL, 0, &err);

//CIImage * img = [CIImage imageWithCGImage:convertedImage];
////[acc setValue:img forKey:@"inputImage"];
////img = [acc valueForKey:@"outputImage"];
//convertedImage = [self.ctx createCGImage:img fromRect:img.extent];

dispatch_sync(dispatch_get_main_queue(), ^{
   self.capturePreviewLayer.contents = (__bridge id)(convertedImage);
});

CGImageRelease(convertedImage);
free(outImage.data);

Upvotes: 1

Views: 404

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90681

Both vImageCreateCGImageFromBuffer() and -[CIContext createCGImage:fromRect:] give you a reference you are responsible for releasing. You are only releasing one of them.

When you replace the value of convertedImage with the new CGImageRef, you are losing the reference to the previous one without releasing it. You need to add another call to CGImageRelease(convertedImage) after your last use of the old image and before you lose that reference.

Upvotes: 0

Related Questions