使用 VTPixelTransferSessionTransferImage 在 Swift 中创建 CVPixelBuffer 时发生内存泄漏
我正在使用 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
}
Any suggestions would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论