开发者

CGContextDrawImage eating my memory, any memory leak?

I'm developing an Mac application on Mac OS X 10.6 using Cocoa and Xcode4, after get image from camera buffer, I need to get the image's raw data. Here is the code:

- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
{
    CVImageBufferRef imageBufferRef;
    @synchronized (self) {
        imageBufferRef = CVBufferRetain(videoFrame);
    }
    if (imageBufferRef) {
        CIImage *originalImage = [CIImage imageWithCVImageBuffer:imageBufferRef];

        // Denoise
        [noiseReductionFilter setValue:originalImage forKey:@"inputImage"];
        CIImage *denoisedImage = [noiseReductionFilter valueForKey:@"outputImage"];
        NSCIImageRep *denoisedImageRep = [NSCIImageRep imageRepWithCIImage:denoisedImage];
        //

        NSImage *image = [[NSImage alloc] initWithSize:[denoisedImageRep size]];
        [image addRepresentation:denoisedImageRep];

        NSUInteger width = CVPixelBufferGetWidth(imageBufferRef);
        NSUInteger height = CVPixelBufferGetHeight(imageBufferRef);
        NSUInteger bytesPerRow = CVPixelBufferGetBytesPerRow(imageBufferRef);
        NSUInteger bitsPerSample = [denoisedImageRep bitsPerSample];

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)[image TIFFRepresentation], NULL);
        CGImageRef imageRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);

        [image release];

        unsigned char *rawData = malloc(height * width * 4);

        NSUInteger bitsPerComponent = 8;

        CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGContextRelease(context);
        CGImageRelease(imageRef);
        CGColorSpaceRelease(colorSpace);

        free(rawData);

        CVBufferRelease(imageBufferRef);
    }
}

(I have removed some code to make it looks more clear, and the code I removed are guaranteed not leaking any memory. And I'm sorry that the variables are not perfectly named.)

The problem is in line:

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

I think after "CGContextRelease(context);", memory should be freed. But after I run this application, the memory drains quickly - increasing around 20MB extra in Real Mem every seconds.

I tried to remove this line of CGContextDrawImage, the memory seems working correctly - only increases tens or hundreds KB per second.

I used Instrument to analyze my app, but no memory leak found.

Anyone can help me to solve this problem? I had worked on it for many days but didn't found any solution, or even the reason.

I'm most appreciate if you can 开发者_C百科give me any suggestion to improve this code or fix any other problems inside it. As even if I remove that line, it still eats memory although slowly, but if I block the entire method, memory won't drain, so I believe there still are some problems in the code.

I'm newbie here, please forgive me if my post isn't in proper format, and my poor English.


Thanks for the hint given by ersentekin, I can now close this question. The answer was:

Found problem! It's in line "CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)[image TIFFRepresentation], NULL);" -- I didn't release "source"!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜