Memory leak creating a CVPixelBuffer in Swift using VTPixelTransferSessionTransferImage

394 Views Asked by At

I am using VTPixelTransferSessionTransferImage to modify the size and pixel format of a CVPixelBuffer. I am struggling to get to the bottom of a memory leak using this code block.

I have found several similar issues but all of the solutions are ObjC, and do not apply in Swift because of the memory management differences. Any help would be much appreciated.

I should note, when this method is called and the size/format already match (when coming from AVFoundation) I do not see a leak, but when CVPixelBuffer comes from a Blackmagic source the leak occurs. Simply returning the sampleBuffer as on iOS results in no leak on macOS with either AVFoundation or Blackmagic sources.

private func convertPixelBuffer(_ sourceBuffer: CVPixelBuffer) -> CVPixelBuffer? {
    #if os(macOS)
    guard let session = pixelTransferSession else { return nil }

    let state = self.state
    var destinationBuffer: CVPixelBuffer? = nil

    let status: CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, state.width, state.height, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, pixelBufferAttributes as CFDictionary?, &destinationBuffer)

    guard status == 0, let destinationBuffer = destinationBuffer else { return nil }

    // transfer the image
    VTPixelTransferSessionTransferImage(session, from: sourceBuffer, to: destinationBuffer)

    return destinationBuffer
    #else
    return sourceBuffer
    #endif
}

enter image description here

Any suggestions would be much appreciated.

0

There are 0 best solutions below