Ali*_*per 5 memory-leaks grand-central-dispatch swift cvpixelbuffer apple-vision
我在捕获管道中使用渲染器类来为视频添加CI过滤器.在渲染器的copyRenderedPixelBuffer中,我还想复制像素缓冲区并将其发送到Vision以检测面部标记.
我使用串行调度队列为Vision创建了一个单例.问题是,一旦我添加了调度队列,pixelBuffer就不会从内存中释放出来 - 因此会出现大量泄漏(即使像素缓冲区在objc代码中释放).随着调度队列静音,内存泄漏消失(但由于Vision功能,视频预览存在大量视频延迟).
任何帮助非常感谢!
- (CVPixelBufferRef)copyRenderedPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
OSStatus err = noErr;
CVPixelBufferRef renderedOutputPixelBuffer = NULL;
CVPixelBufferRef visionOutputPixelBuffer;
CIImage *sourceImage = nil;
err = CVPixelBufferPoolCreatePixelBuffer( kCFAllocatorDefault, _bufferPool, &renderedOutputPixelBuffer );
if ( err ) {
NSLog(@"Cannot obtain a pixel buffer from the buffer pool (%d)", (int)err );
goto bail;
}
err = CVPixelBufferPoolCreatePixelBuffer( kCFAllocatorDefault, _bufferPool, &visionOutputPixelBuffer );
if ( err ) {
NSLog(@"Cannot obtain a pixel buffer from the buffer pool (%d)", (int)err );
}
// Vision
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
int bufferHeight = (int)CVPixelBufferGetHeight(pixelBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(pixelBuffer);
CVPixelBufferLockBaseAddress(visionOutputPixelBuffer, 0);
uint8_t *copyBaseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(visionOutputPixelBuffer);
memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow);
CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
CVPixelBufferUnlockBaseAddress(visionOutputPixelBuffer,0);
[[VisionFaceDetection sharedInstance] detectFaceOnPixelBuffer:visionOutputPixelBuffer];
CVPixelBufferRelease(visionOutputPixelBuffer);
// ... Other Filter code here ...
bail:
if(sourceImage != nil)
{
[sourceImage release];
}
return renderedOutputPixelBuffer;
}
Run Code Online (Sandbox Code Playgroud)
Vision的Swift类
@objc final class VisionFaceDetection: NSObject {
@objc static let sharedInstance = VisionFaceDetection() // singleton so it can handle the async dispatch
private override init() {}
let serialQueue = DispatchQueue(label: "vision", qos: DispatchQoS.userInitiated)
let faceLandmarks = VNDetectFaceLandmarksRequest()
let faceLandmarksDetectionRequest = VNSequenceRequestHandler()
// CVPixelBuffer
@objc func detectFace(onPixelBuffer pixelBuffer: CVPixelBuffer) {
// Currently this block causes a memory leak with the pixel buffer
serialQueue.async {
let faceDetectionRequest = VNDetectFaceRectanglesRequest { [weak self] (request, error) in
guard let observations = request.results as? [VNFaceObservation] else {
print("Unexpected result type from face detection request")
return
}
// happens even when these are muted
// self?.faceLandmarks.inputFaceObservations = observations
// self?.detectLandmarks(onPixelBuffer: pixelBuffer)
}
let faceDetectionRequestHandler = VNSequenceRequestHandler()
try? faceDetectionRequestHandler.perform([faceDetectionRequest], on: pixelBuffer)
}
}
// ... other methods in class
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
392 次 |
| 最近记录: |