我對我的WPF代碼中的內存泄漏有點困惑。我將一些3D幾何渲染爲幾個RenderTargetBitmaps,然後將其渲染到一個大的主RenderTargetBitmap。但是當我這樣做時,我會在一兩分鐘後得到一個內存泄漏,使我的應用程序崩潰。使用RenderTargetBitmap的WPF內存泄漏?
我在下面的簡化代碼中重現了錯誤。
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);
}
任何人都可以看到任何明顯的錯誤嗎?爲什麼這段代碼會有嚴重的內存泄漏?
我覺得我看到這一點。你會得到什麼例外? – Gleno 2013-04-04 19:29:39