BitmapEncoder MemoryLeak with SoftwareBitmap
BitmapEncoder MemoryLeak with SoftwareBitmap
我使用 BitmapEncoder
class 将捕获的摄像机视频帧转换为流。当使用 DispatcherTimer 在循环中执行此操作时,我的应用程序逐渐泄漏内存但从未恢复任何内存。
我使用 this Microsoft 示例作为基础。
泄漏的实际代码。
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
using (var videoFrame = new VideoFrame(BitmapPixelFormat.Rgba16, (int)previewProperties.Width, (int)previewProperties.Height))
{
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame
using (SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap)
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
//MEMORY LEAK
encoder.SetSoftwareBitmap(previewFrame);
await encoder.FlushAsync();
//DO something with the stream
}
}
}
}
在更加努力地搜索并浏览 MSDN 论坛后,我发现 MSDN thread 有完全相同的问题。这是 Visual Studio 中的一个错误,只会在调试时影响应用程序。
我使用 BitmapEncoder
class 将捕获的摄像机视频帧转换为流。当使用 DispatcherTimer 在循环中执行此操作时,我的应用程序逐渐泄漏内存但从未恢复任何内存。
我使用 this Microsoft 示例作为基础。
泄漏的实际代码。
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
using (var videoFrame = new VideoFrame(BitmapPixelFormat.Rgba16, (int)previewProperties.Width, (int)previewProperties.Height))
{
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame
using (SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap)
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
//MEMORY LEAK
encoder.SetSoftwareBitmap(previewFrame);
await encoder.FlushAsync();
//DO something with the stream
}
}
}
}
在更加努力地搜索并浏览 MSDN 论坛后,我发现 MSDN thread 有完全相同的问题。这是 Visual Studio 中的一个错误,只会在调试时影响应用程序。