我有基于此示例的代码;https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraGetPreviewFrame。每次都行;
await _mediaCapture.GetPreviewFrameAsync(_currentVideoFrame);
被击中我们似乎泄漏了内存。我没有收拾什么?我也尝试过每次都创建模板框架并在每个循环上对其进行处理和归零——这似乎也不起作用。
我已经回到微软的原始样本,它似乎也泄漏了。这是我的代码;
await Task.Run(async () =>
{
try
{
var videoEncodingProperties =
_mediaCapture.VideoDeviceController.GetMediaStreamProperties
(MediaStreamType.VideoPreview) as VideoEncodingProperties;
Debug.Assert(videoEncodingProperties != null, nameof(videoEncodingProperties) + " != null");
_currentVideoFrame = new VideoFrame(BitmapPixelFormat.Gray8,
(int) videoEncodingProperties.Width,
(int) videoEncodingProperties.Height);
TimeSpan? lastFrameTime = null;
while (_mediaCapture.CameraStreamState == CameraStreamState.Streaming)
{
token.ThrowIfCancellationRequested();
await _mediaCapture.GetPreviewFrameAsync(_currentVideoFrame);
if (!lastFrameTime.HasValue ||
lastFrameTime != _currentVideoFrame.RelativeTime)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(CoreDispatcherPriority.Normal,
() =>
{
try
{
Debug.Assert(_currentVideoFrame != null,
$"{nameof(_currentVideoFrame)} != null");
var bitmap = _currentVideoFrame.SoftwareBitmap.AsBitmap();
float focalLength = _cameraOptions == CameraOptions.Front ? AppSettings.FrontCameraFocalLength : AppSettings.RearCameraFocalLength;
_frameProcessor.ProcessFrame(bitmap, focalLength);
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex}");
}
});
lastFrameTime = _currentVideoFrame.RelativeTime;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex}");
}
},
token);
这应该只是获取帧并将它们通过_frameProcessor.ProcessFrame()
调用,但即使它什么都不做(我删除了除 GetPreviewFrameAsync 之外的所有内容)它也会泄漏。
要重复该问题,请从以下位置下载示例;https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraGetPreviewFrame。在 Windows 10 v 1903 (18362.175) 下使用诊断工具 (Debug->Windows->Show Diagnostic tools) 远程运行调试器中的示例到 Surface Pro 4 (i5-6300U @2.4GHz)。打开显示帧复选框并在按下 GetPreviewFrameAsync 按钮时观察内存。记忆如下所示,每次上升都是我按下按钮;