fix memory leak on macOS (#9)

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-03-14 12:44:36 +08:00 • committed by GitHub
parent f15e5eee20
commit b91e1b2092
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 35 deletions

View file

@ -32,38 +32,52 @@ import CoreVideo
return Unmanaged.passRetained(data!)
}
}
private func _markFrameAvaliable(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
var pixelBufferCopy: CVPixelBuffer?
// macOS only support 32BGRA currently.
let dict: [String: Any] = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferMetalCompatibilityKey as String: true,
kCVPixelBufferOpenGLCompatibilityKey as String: true,
// https://developer.apple.com/forums/thread/712709
kCVPixelBufferBytesPerRowAlignmentKey as String: 64
]
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy)
guard result == kCVReturnSuccess else {
return false
}
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
memcpy(ptr, buffer, len)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
self.data = pixelBufferCopy
self.width = width
self.height = height
if textureId != -1 && self.data != nil {
registry?.textureFrameAvailable(textureId)
return true
} else {
return false
}
}
@objc public func markFrameAvaliableRaw(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
queue.sync {
_markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align)
}
}
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool {
queue.sync {
self.data = data.withUnsafeBytes { buffer in
var pixelBufferCopy: CVPixelBuffer!
// macOS only support 32BGRA currently.
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [
kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,
kCVPixelBufferMetalCompatibilityKey: true,
kCVPixelBufferOpenGLCompatibilityKey: true,
// https://developer.apple.com/forums/thread/712709
kCVPixelBufferBytesPerRowAlignmentKey: 64
] as CFDictionary, &pixelBufferCopy)
guard result == kCVReturnSuccess else {
return nil
}
CVPixelBufferLockBaseAddress(pixelBufferCopy, [])
let source = buffer.baseAddress!
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
memcpy(ptr, source, data.count)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, [])
return pixelBufferCopy
}
self.width = width
self.height = height
if (textureId != -1 && self.data != nil) {
registry?.textureFrameAvailable(textureId)
return true
} else {
return false
}
data.withUnsafeBytes { buffer in
markFrameAvaliableRaw(
buffer: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
len: data.count,
width: width,
height: height,
stride_align: stride_align)
}
}
}

View file

@ -15,8 +15,7 @@ extern "C" {
/// Keep the same name with Windows.
void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int len, int width, int height, int stride_align) {
TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr);
NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:len freeWhenDone:FALSE];
[texture_rgba markFrameAvaliableWithData:data width:width height:height stride_align: stride_align];
[texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: stride_align];
}
#if __cplusplus