title: CGContextDrawImage eating my memory, any memory leak?
body: <p>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:</p>
  
  <pre><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);
      }
  }
  </code></pre>
  
  <p>(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.)</p>
  
  <p>The problem is in line:</p>
  
  <pre><code>CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  </code></pre>
  
  <p>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.</p>
  
  <p>I tried to remove this line of CGContextDrawImage, the memory seems working correctly - only increases tens or hundreds KB per second.</p>
  
  <p>I used Instrument to analyze my app, but no memory leak found.</p>
  
  <p>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.</p>
  
  <p>I'm most appreciate if you can 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.</p>
  
  <p>I'm newbie here, please forgive me if my post isn't in proper format, and my poor English.</p>