title: iOS CVPixelBufferCreate leaking memory in swift 2
body: <p>I'm trying to convert an image into a video, and the right way seems to use a AVAssetWriter with a AVAssetWriterInputPixelBufferAdaptor, and it works well, but it leaks memory.</p>
  
  <p>When I convert the CGImage to a CVPixelBuffer, I call CVPixelBufferCreate, which never frees it's memory.</p>
  
  <pre><code>func CGImageToPixelBuffer(image: CGImageRef, frameSize: CGSize) -&gt; CVPixelBuffer {
  
      // stupid CFDictionary stuff
      let keys: [CFStringRef] = [kCVPixelBufferCGImageCompatibilityKey, kCVPixelBufferCGBitmapContextCompatibilityKey]
      let values: [CFTypeRef] = [kCFBooleanTrue, kCFBooleanTrue]
      let keysPointer = UnsafeMutablePointer&lt;UnsafePointer&lt;Void&gt;&gt;.alloc(1)
      let valuesPointer =  UnsafeMutablePointer&lt;UnsafePointer&lt;Void&gt;&gt;.alloc(1)
      keysPointer.initialize(keys)
      valuesPointer.initialize(values)
      let options = CFDictionaryCreate(kCFAllocatorDefault, keysPointer, valuesPointer, keys.count,
          UnsafePointer&lt;CFDictionaryKeyCallBacks&gt;(), UnsafePointer&lt;CFDictionaryValueCallBacks&gt;())
  
      let buffer = UnsafeMutablePointer&lt;CVPixelBuffer?&gt;.alloc(1)
  
      // here's the leak &gt;:[
      let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(frameSize.width), Int(frameSize.height),
          kCVPixelFormatType_32ARGB, options, buffer)
  
      CVPixelBufferLockBaseAddress(pixelBuffer.memory!, 0);
      let bufferData = CVPixelBufferGetBaseAddress(buffer.memory!);
  
      let rgbColorSpace = CGColorSpaceCreateDeviceRGB();
      let context = CGBitmapContextCreate(bufferData, Int(frameSize.width),
          Int(frameSize.height), 8, 4*Int(frameSize.width), rgbColorSpace,
          CGImageAlphaInfo.NoneSkipFirst.rawValue);
  
      CGContextDrawImage(context, CGRectMake(0, 0,
          CGFloat(CGImageGetWidth(image)),
          CGFloat(CGImageGetHeight(image))), image);
  
      CVPixelBufferUnlockBaseAddress(pixelBuffer.memory!, 0);
      return buffer.memory!
  }
  </code></pre>
  
  <p>And here's the code which calls CGImageToPixelBuffer</p>
  
  <pre><code>func saveImageAsVideoFile(path: NSURL, image: UIImage, duration: Double) {
      let writer = try! AVAssetWriter(URL: path, fileType: AVFileTypeQuickTimeMovie)
  
      let videoSettings: NSDictionary = [
          AVVideoCodecKey : AVVideoCodecH264,
          AVVideoWidthKey : NSNumber(integer: Int(image.size.width)),
          AVVideoHeightKey : NSNumber(integer: Int(image.size.height))
      ]
      let input = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings as? [String : AnyObject])
      let inputAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input, sourcePixelBufferAttributes: nil)
      input.expectsMediaDataInRealTime = false
      writer.addInput(input)
      writer.startWriting()
      writer.startSessionAtSourceTime(kCMTimeZero)
  
      // leak starts here
      let buffer = CGImageToPixelBuffer(image.CGImage!, frameSize: image.size)
  
      // append 30 frames to AVAssetWriter thing
      for i in 0..&lt;30
          if input.readyForMoreMediaData {
              inputAdaptor.appendPixelBuffer(buffer, withPresentationTime: CMTimeMake(i, 30))
          }
      }
  
      //CVPixelBufferRelease(buffer) ??
  
      input.markAsFinished()
      writer.finishWritingWithCompletionHandler({ () -&gt; Void in
          print("yay")
      })
  }
  </code></pre>
  
  <p>CVPixelBufferRelease is not available in swift 2</p>
  
  <p>CVPixelBufferCreate doesn't return a unmanaged pointer in swift 2, so I can't use this <a href="https://stackoverflow.com/questions/25611086/cvpixelbufferpool-error-kcvreturninvalidargument-6661">guys code</a> </p>
  
  <p>I've tried manually calling destroy and dealloc on the unsafepointer, to no avail. </p>
  
  <p>every time it's called it increases the memory usage, and will crash the device if called enough
  <a href="https://i.stack.imgur.com/scD73.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/scD73.png" alt="memory profile"></a></p>
  
  <p>Any help or advice would be appreciated.</p>