mvvmlight icon indicating copy to clipboard operation
mvvmlight copied to clipboard

Memory leak caused by the `Text` property

Open agentS opened this issue 5 years ago • 0 comments

I am extending a debugger and I need to open mutliple source code editor instances in external windows. Furthermore, correct disposal is an absolute necessity. However, the following line causes a memory leak resulting in the view not being disposed, whereas the view model is:

<TextBox  Grid.Row="0" Grid.Column="1" Text="{Binding MyBinding, Mode=OneWayToSource}" MinWidth="20"></TextBox>

The setup is a little bit special, admittedly: the Text binding occurs in a MVVM window, which is created using the following code (for better understanding, I created a sketch):

// Launcher
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    if (this.mvvmWindow != null)
    {
        this.mvvmWindow.Close();
    }

    this.mvvmWindow = new MVVMWindow();
    this.mvvmWindow.Closed += this.CleanupMVVMWindow;
    this.mvvmWindow.Show();
}

private void CleanupMVVMWindow(Object sender, EventArgs e)
{
    this.mvvmWindow.Closed -= this.CleanupMVVMWindow;
    this.mvvmWindow = null;

    GC.Collect();
    GC.WaitForFullGCComplete();
}

For debugging this behaviour I have created an example application with exactly that memory leak. Just run the application in a profiler like dotMemory and you'll see that the view stays alive after closing the window.

agentS avatar Aug 31 '18 14:08 agentS