... Home Contact

Krzysztof Koźmic's blog

Designed in Poland, assembled in Australia.


Show appreciation: My Amazon.com Wish List


Me@Twitter

    Currently reading

    Article Categories

    Archives

    Post Categories

    MyPersonal

    Syndication:

    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:

    Feedback

    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Tuna Toksz | 3/11/2009 10:20 PM

    Thanks for the tip!
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Larissa Fedora | 5/14/2009 2:47 PM

    Hi Kozmic,

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

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Krzysztof Kozmic | 5/14/2009 5:00 PM

    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).
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Larissa Fedora | 5/15/2009 4:21 AM

    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?
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Krzysztof Kozmic | 5/15/2009 4:59 PM

    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).
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Larissa Fedora | 5/19/2009 12:30 PM

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

    Thanks, Kozmic :)
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Larissa Fedora | 6/17/2009 4:27 AM

    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.
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Pedro | 1/21/2010 12:43 PM

    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.
    Gravatar

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Krzysztof Koźmic | 1/21/2010 5:20 PM

    Pedro,

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

    # re: WPF memory leak with VisualBrush TileMode="Tile"
    Pedro | 2/18/2010 11:10 AM

    Thanks for the answer,

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



    Comments have been closed on this topic.