WPFコードのメモリリークに少し困惑しています。いくつかの3DジオメトリをいくつかのRenderTargetBitmapにレンダリングしてから、それぞれを大きなマスターRenderTargetBitmapにレンダリングしています。しかし、これを行うと、メモリリークが発生し、わずか1〜2分後にアプリがクラッシュします。
次の簡略化されたコードでエラーを再現しました。
private void timer1_Tick(object sender, EventArgs e) {
// if first time, create final stitch bitmap and set UI image source
if (stitch == null) {
stitch = new RenderTargetBitmap(1280, 480, 96, 96, PixelFormats.Pbgra32);
myImage.Source = stitch;
}
// create visual and render to img1
Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
DrawingVisual dvis = new DrawingVisual();
using (DrawingContext dc = dvis.RenderOpen()) {
dc.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
}
RenderTargetBitmap img1 = new RenderTargetBitmap(640, 480, 96, 96, PixelFormats.Pbgra32);
img1.Render(dvis);
// create visual and render to final stitch
DrawingVisual vis = new DrawingVisual();
using (DrawingContext dc = vis.RenderOpen()) {
dc.DrawImage(img1, new Rect(0, 0, 640, 480));
}
stitch.Clear();
stitch.Render(vis);
}
誰かがここでうまくいかない明らかな何かを見ることができますか?このコードでひどいメモリリークが発生するのはなぜですか?