WPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak

StackOverflow https://stackoverflow.com/questions/19200104

سؤال

In my application, I have some user controls that open in the main window. I suppose to open the user controls and close them. In each user control, I have pictures , textboxes and label like any information form. I noticed that memory doesn't reclaim by garbage collector.I have searched the internet for a weak to resolve memory leak of my application. I found some good answers to my question, but most of them related to .net framework 3.5 or earlier. I used memory profiler to find the clue based on some advises. When I used memory profiler, I found a lot weak references caused by binding to object. This is according with link and I tried to remove the binding just before removing parent control, and It didn't solve my problem. here xaml code:

<TextBox x:Name="nameTextBox" Text="{Binding Name, Mode=TwoWay}"  />

I Set DataContext to an instance of my Model. My model like follows

 public class MyModel : INotifyPropertyChanged
 {
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name!= value)
            {
                name= value;
                OnProperyChanged("Name");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnProperyChanged(string propertyName)
    {
        if (PropertyChanged != null)
           this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }`
    ...
}

I tried to clear my binding using following code:

    BindingOperations.ClearBinding(nameTextBox,TextBox.TextProperty);

but weak reference didn't go away. I found a useful code project page , but it is for event handlers. At the end, if I misunderstand the links that I mentioned, Please give me a clue that what I have to do? If there is another solution for binding.

Note: I have seen the Link and I know I have to get rid of following elements:

  • Event handlers to objects in parent windows
  • Registering to events from static objects
  • Using timers
  • Data binding
  • Changing the Text property of a text box
هل كانت مفيدة؟

المحلول

The idea behind the weak reference is that you can keep a reference to an object, without preventing it to get garbage collected. In stead, when the object pointed to is collected, all weak references are set to null.

I have all confidence that this actually works. So if you are looking for the root cause for an object not being collected, you can safely ignore the Weak References. The WPF library objects may keep other non-weak references that are causing problems, and there are documented situations where this happens.

I used to have code to recursively remove all databindings, which had some ugly side effects so I asked this question. I can recommend you to follow the advice in @Kent's answer: make sure all binding is done relative to a single (or few) DataContext properties, and set those to null. That should cleanly disconnect all WPF bindings depending on that DataContext.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top