swift 是否管理我从 CVPixelBufferCreate 创建的 CVPixelBuffer 的内存?

Do swift manage the memory of a CVPixelBuffer that I create from CVPixelBufferCreate?

假设我想存储来自相机输出的帧

let imageBuffer:CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
some_list.append(imageBuffer.copy())

**

下面是复制函数是如何通过对 CVPixelBuffer 的扩展来定义的

extension CVPixelBuffer {
    func copy() -> CVPixelBuffer {
        precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")
        var _copy : CVPixelBuffer?
        CVPixelBufferCreate(
            nil,
            CVPixelBufferGetWidth(self),
            CVPixelBufferGetHeight(self),
            CVPixelBufferGetPixelFormatType(self),
            CVBufferGetAttachments(self, CVAttachmentMode.shouldPropagate),
            &_copy)
        guard let copy = _copy else { fatalError() }
        CVPixelBufferLockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
        CVPixelBufferLockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
        let dest = CVPixelBufferGetBaseAddress(copy)
        let source = CVPixelBufferGetBaseAddress(self)
        let height = CVPixelBufferGetHeight(self)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(self)
        memcpy(dest, source, height * bytesPerRow)
        CVPixelBufferUnlockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
        CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
        return copy
    }
}

问题是,我是否需要显式管理我创建的 CVPixelBuffer 副本?还是 swift 通过引用计数处理它?

Swift 管理您的缓冲区对象,因此您不必考虑释放它。

Core Foundation objects returned from annotated APIs are automatically memory-managed in Swift—you don't need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself.

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/working_with_core_foundation_types#2994152

事实上,CVPixelBufferRelease 函数没有 Swift 版本。

https://developer.apple.com/documentation/corevideo/1563589-cvpixelbufferrelease