J.Olsson
J.Olsson

Reputation: 815

Does this OneWay Binding leak memory?

I Am using a MVVM pattern in a WPF application and in educational purposes I have a question about Memory leaks.

I been using the Postsharp library.

My ViewModel looks like following:

public class MyViewModel : INotifyPropertyChanged
{
     private ClassA _myClassAField; //ClassA doesn't Implement INotifyPropertyChanged

     public MyViewModel(ClassA classAParameter)
     {
          _myClassAField = classAParameter;
     }

     [NotifyPropertyChanged]
     public string Name { get { return _myClassAField.Name; } }


     .....//Other Code.....    
}

Xaml:

<Textblock Text="{Binding Name, Mode=OneWay}"/>

Question:

Does this binding leak memory? In my testing it seems not to be leaking, but i do not understand why?

The binding to Property Name is only a getter from the ClassA that don't implement INotifyPropertyChanged.

Upvotes: 2

Views: 798

Answers (1)

Natxo
Natxo

Reputation: 2947

Your ClassA doesn't implement INotifyPropertyChanged but you are binding to a property in the class MyViewModel, which does! Therefore you should not have memory leaks.

For reference: https://blogs.msdn.microsoft.com/micmcd/2008/03/07/avoiding-a-wpf-memory-leak-with-databinding-black-magic/

There is an issue where WPF checks to find things that implement INotifyProperyChanged. If there is a databinding to something not implementing this interface, then it makes a record in a global table. That record doesn’t get cleaned up, as WPF has no way of checking when that DB record is no longer needed.

Upvotes: 3

Related Questions