0

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.

DanM
  • 121
  • 8
  • When you profile in instruments, do you see which object actually leaks? – timbre timbre Mar 13 '22 at 19:54
  • It definitely appears to be the created buffer from what the trace shows, or at least I think so. – DanM Mar 13 '22 at 20:05
  • Do you destroy transfer session t some point, like docs say (https://developer.apple.com/documentation/videotoolbox/vtpixeltransfersession-7cg): `When you finish with the pixel transfer session, call VTPixelTransferSessionInvalidate(_:) to tear it down, and CFRelease to free its memory.` – timbre timbre Mar 13 '22 at 20:12
  • Yes. That session has the lifetime of the capture model however, so it is present throughout the capture session, otherwise it would be allocated every sample buffer. – DanM Mar 13 '22 at 20:36

0 Answers0