views:

22

answers:

0

Our application adds and removes WPF (4.0) UserControls from it's main window, and we've noticed that they are not getting Garbage Collected (via WeakReferences that never return null). It seems that if a UserControl has a binding this occurs, even if the control was never added to the tree. The simplest reproduction in a console application follows:

Main Program

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var obj = GetObj();
        var ctl = GetCtl();
        for (int x = 0; x < 4; x++)
        {
            GC.Collect();
            Thread.Sleep(1000);
        }
        bool objAlive = obj.IsAlive;
        bool ctlAlive = ctl.IsAlive;
        Console.WriteLine(objAlive + "-" + ctlAlive);
        Console.ReadLine();
    }

    private static WeakReference GetObj()
    {
        return new WeakReference(new object());
    }

    private static WeakReference GetCtl()
    {
        return new WeakReference(new MyCtl());
    }
}

MyCtl User Control (Code behind is standard and has not been modified

<UserControl <!--Snip Default Namespaces-->>
    <TextBlock Text="{Binding Blah}" />
</UserControl>

objAlive is false, but ctlAlive is true. If the Binding is removed from the UserControl XAML, both are false. If I connect a memory profiler, MyCtl is still hanging around, but I can't figure out what is referencing it (tips on how to find this using the jetBrains one is appreciated).

Is this expected behaviour or do I need to do more to clean up my WPF UserControls?