Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak
    primarykey
    data
    text
    <p>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 <a href="https://stackoverflow.com/questions/7737070/prevent-memory-leaks-in-wpf">link</a> and I tried to remove the binding just before removing parent control, and It didn't solve my problem. here xaml code:</p> <pre><code>&lt;TextBox x:Name="nameTextBox" Text="{Binding Name, Mode=TwoWay}" /&gt; </code></pre> <p>I Set DataContext to an instance of my Model. My model like follows</p> <pre><code> 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)); }` ... } </code></pre> <p>I tried to clear my binding using following code:</p> <pre><code> BindingOperations.ClearBinding(nameTextBox,TextBox.TextProperty); </code></pre> <p>but weak reference didn't go away. I found a useful <a href="http://www.codeproject.com/Articles/29922/Weak-Events-in-C#heading0010" rel="nofollow noreferrer">code project page</a> , 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. </p> <p>Note: I have seen the <a href="https://stackoverflow.com/questions/18542940/wpf-can-bindings-create-memory-leaks">Link</a> and I know I have to get rid of following elements:</p> <ul> <li>Event handlers to objects in parent windows</li> <li>Registering to events from static objects</li> <li>Using timers</li> <li>Data binding</li> <li>Changing the Text property of a text box</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload