Recently at work, while working on WPF GUI for our application we noticed, that our app leaks memory. After some time (actually a lot of time, since we first though that it was video codec’s fault) we nailed it, and unfortunately, it looks like it’s a bug in the WPF itself. I attach a sample application that exposes the issue.
What it does is basically… blinking a rectangle. The key thing is, how the rectangle is declared:
<Rectangle Name="ResultsBorder" Height="100" Width="100" Opacity="0.0">
<Rectangle.Fill>
<DrawingBrush TileMode="Tile" Stretch="None" Viewport="0,0,1,4" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#C0FFFFFF">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,2"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="#90FFFFFF">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,2,1,2"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
The critical part that causes the leak (and ultimately OutOfMemoryException) is using DrawingBrush with TileMode set to Tile. Changing this, makes the issue go away. It was tested on few computers, running Intel, AMD, and NVidia GPUs. The issue exists on Windows XP, as well as on Windows 7.
There is a Connect entry for that issue, but so far it is unresolved.
The workaround for now, is to not use DrawingBrush, which is what we did.
If you want to check the issue for yourself here’s the code.