开发者

Why does a WPF UserControl with a binding not get Garbage Collected?

开发者 https://www.devze.com 2023-01-11 23:44 出处:网络
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 tha

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 WeakRefere开发者_如何学JAVAnce(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?


Depends on what you are binding to.

This could be a known issue: http://support.microsoft.com/kb/938416

Are you binding to a custom class? Have you implemented INotifyPropertyChanged or made a DependencyProperty?

0

精彩评论

暂无评论...
验证码 换一张
取 消