WPF memory leak with VisualBrush TileMode="Tile"

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.

leaking_app

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.

Technorati Tags:

Comments

Tuna Toksz says:

Thanks for the tip!

Larissa Fedora says:

Hi Kozmic,

Any hotfix released by microsoft regarding this defect? Have the same problem here.

Larissa,

As you can see in the connect site Microsoft wrote: "We recently fixed a leak that involved tiled VisualBrushes, and this problem seems to have gone away."

No info on when the fix will be available thought (my guess – .NET 4.0).

Larissa Fedora says:

Hi Kozmic,

Thanks for your prompt reply.

The memory leak does happen in Windows XP, but when i try to run the same application in Vista, the memory leak is not that bad compare to XP. Any idea?

We have noticed that as well. I suppose it’s somehow related to internal architecture of graphics pipeline in OS but it’s only a wild guess. The fact is that it’s still there (even on Win7 beta).

Larissa Fedora says:

Seems that i need to either wait for the release hotfix or totally not use TileMode=Tile in VisualBrush.

Thanks, Kozmic 🙂

Larissa Fedora says:

FYI, I’ve tested it in PCs with various type of NVidia graphic card (Quadro FX540 and F560), the leak was occuring. But however, when i test my app in PC with Radeon, the leak seems not occuring at all. Updating the NVidia driver with the latest doesn’t eliminate the leak too.

Pedro says:

I am having the same problem with the visual brushes.

Has anyone knows where i can find a hotfix for this bug, or a workaround.

Thanks.

Pedro,

I don’t think there’s any other than "don’t use it"

Pedro says:

Thanks for the answer,

Do you know a control that i can use to simulate the same behaviour of the tile visualbrush?..