使用 VTPixelTransferSessionTransferImage 在 Swift 中创建 CVPixelBuffer 时发生内存泄漏

发布于 2025-01-13 17:11:52 字数 1278 浏览 2 评论 0原文

我正在使用 VTPixelTransferSessionTransferImage 来修改 CVPixelBuffer 的大小和像素格式。我正在努力使用此代码块找出内存泄漏的根源。

我发现了几个类似的问题,但所有的解决方案都是 ObjC,并且由于内存管理差异而不适用于 Swift。任何帮助将不胜感激。

我应该注意,当调用此方法并且大小/格式已经匹配时(来自 AVFoundation 时),我没有看到泄漏,但是当 CVPixelBuffer 来自 Blackmagic 源时,就会发生泄漏。只需像在 iOS 上一样返回 sampleBuffer ,就不会在 macOS 上使用 AVFoundation 或 Blackmagic 源造成泄漏。

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
}

输入图片此处描述

如有任何建议,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文