title: How to fix leak CVPixelBuffer
body: <p>please tell me where is leak  in this code... </p>
  
  <p>//here I did video with images from Document Directory</p>
  
  <pre><code>- (void) testCompressionSession:(NSString *)path
  {
  if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
      [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  }
  NSArray *array = [dictInfo objectForKey:@"sortedKeys"];
  
  NSString *betaCompressionDirectory = path;
  NSError *error = nil;
  
  unlink([betaCompressionDirectory UTF8String]);
  
  NSLog(@"array = %@",array);
  NSData *imgDataTmp = [NSData dataWithContentsOfFile:[projectPath stringByAppendingPathComponent:[array objectAtIndex:0]]];
  NSLog(@"link : %@",[projectPath stringByAppendingPathComponent:[array objectAtIndex:0]]);
  CGSize size = CGSizeMake([UIImage imageWithData:imgDataTmp].size.width, [UIImage imageWithData:imgDataTmp].size.height);
  //----initialize compression engine
  NSLog(@"size : w : %f, h : %f",size.width,size.height);
  AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:betaCompressionDirectory]
                                                         fileType:AVFileTypeQuickTimeMovie
                                                            error:&amp;error];
  NSParameterAssert(videoWriter);
  if(error)
      NSLog(@"error = %@", [error localizedDescription]);
  
  NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
                                 [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                 [NSNumber numberWithInt:size.height], AVVideoHeightKey, nil];
  AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
  
  NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                         [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
  
  AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                                                                                   sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
  NSParameterAssert(writerInput);
  NSParameterAssert([videoWriter canAddInput:writerInput]);
  
  if ([videoWriter canAddInput:writerInput])
      NSLog(@"I can add this input");
  else
      NSLog(@"i can't add this input");
  
  [videoWriter addInput:writerInput];
  
  [videoWriter startWriting];
  [videoWriter startSessionAtSourceTime:kCMTimeZero];
  
  dispatch_queue_t    dispatchQueue = dispatch_queue_create("mediaInputQueue", NULL);
  
  [writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
      //BOOL isEffect = NO;
      int i = 0;
      float totalTime = 0.0f;
      float nextTime = 0;
      if ([writerInput isReadyForMoreMediaData]) {
          while (1)
          {   
              if (i &lt;= [array count] &amp;&amp; i &gt; 0) {
                  nextTime = [[dictInfo objectForKey:[array objectAtIndex:i-1]] floatValue];
              }
              totalTime += i == 0 ? 0 : nextTime;
              CMTime presentTime=CMTimeMake(totalTime, 1);
              printf("presentTime : %f  ",CMTimeGetSeconds(presentTime));
              if (i &gt;= [array count]) 
              {
                  NSData *imgData = [NSData dataWithContentsOfFile:[projectPath stringByAppendingPathComponent:[array objectAtIndex:i-1]]];
                  UIImage* tmpImg = [UIImage imageWithData:imgData];
                  tmpImg = [self imageWithImage:tmpImg scaledToSize:size];
                  while ( !writerInput.readyForMoreMediaData)
                  {
                      sleep(0.01);
                  }
                  CVPixelBufferRef buffer = NULL;
                  buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[tmpImg CGImage] size:size];
                  [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(totalTime-nextTime+(nextTime/2.0), 1)];
                  NSLog(@"%f",totalTime-nextTime+(nextTime/2.0));
                  [writerInput markAsFinished];
                  [videoWriter finishWriting];
                  //CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
                  [videoWriter release];
                  break;
              } else {
                  NSData *imgData = [NSData dataWithContentsOfFile:[projectPath stringByAppendingPathComponent:[array objectAtIndex:i]]];
                  UIImage* tmpImg = [UIImage imageWithData:imgData];
                  //tmpImg = [self imageWithImage:tmpImg scaledToSize:size];
                  //UIImageWriteToSavedPhotosAlbum(tmpImg, nil, nil, nil);
                  while (!adaptor.assetWriterInput.readyForMoreMediaData &amp;&amp; !writerInput.readyForMoreMediaData)
                  {
                      sleep(0.01);
                  }
                  CVPixelBufferRef buffer = NULL;
                  buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[tmpImg CGImage] size:size];
                  if (buffer)
                  {
                      if(![adaptor appendPixelBuffer:buffer withPresentationTime:presentTime])
                          NSLog(@"FAIL");
                      else
                          NSLog(@"Success:%d",i);
                      CVPixelBufferRelease(buffer);
                  }
              }
      i++;
          }
      }
  }];
  </code></pre>
  
  <p>//and here I did CVPixelBufferRef from CGImageRef</p>
  
  <pre><code>- (CVPixelBufferRef )pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
  {
  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, 
                           [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
  CVPixelBufferRef pxbuffer = NULL;
  
  CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, &amp;pxbuffer);
  
  NSParameterAssert(status == kCVReturnSuccess &amp;&amp; pxbuffer != NULL); 
  
  CVPixelBufferLockBaseAddress(pxbuffer, 0);
  void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
  NSParameterAssert(pxdata != NULL);
  
  CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
  NSParameterAssert(context);
  
  CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
  CGColorSpaceRelease(rgbColorSpace);
  CGContextRelease(context);
  
  CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
  return pxbuffer;
  </code></pre>
  
  <p>leak log is :</p>
  
  <p>CVObject    CFRetain    00:37.957.985   2   0x1ecae0    0   CoreVideo   CVPixelBufferPool::createPixelBuffer(__CFAllocator const*, __CFDictionary const*, int*)
  Malloc 96 Bytes Malloc  00:40.015.872   1   0x1f0750    96  CoreVideo   CVBuffer::init()
  CVPixelBuffer   Malloc  00:40.969.716   1   0x1f2570    96  CoreVideo   CVObject::alloc(unsigned long, __CFAllocator const*, unsigned long, unsigned long) </p>