I use the BitmapEncoder
class for converting a captured videoframe of a camera to a Stream. When doing this in a loop using a DispatcherTimer my application gradually leaks memory but never recovers any of it.
I used this Microsoft sample as base.
The actual code that leaks.
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
}
}
}
}
After searching harder and browsing through the MSDN forums I found a MSDN thread with exact the same issue. It's a bug in Visual Studio and does only affect apps while debugging them.