Do I need to unbind items as the item disappears in order to prevent memory leaks? I guess I\'m just a little worried that if I reload and a new template is applied to a con
Not pretend to answer, just for reference. In a classic article on Finding Memory Leaks in WPF-based applications author Jossef Goldberg, described in detail cases, where there may be a memory leak in WPF application. Really, most relate to the .NET 3.5/4.0, but some cases may be relevant to this day. Also, have a small extension.
Quote about leak in Binding:
Cause:
This leak documented in this kb article. It is triggered because:
The TextBlock control has a binding to an object (myGrid) that has a reference back to the TextBlock (it is one of myGrid children’s).
Note: that this type of a DataBinding leak is unique to a specific scenario (and not to all DataBinding scenarios) as documented in the kb article. The property in the Path is a not a DependencyProperty and not on a class which implements INotifyPropertyChanged and in addition a chain of strong reverences must exist.
Code:
myDataBinding = new Binding("Children.Count");
myDataBinding.Source = myGrid;
myDataBinding.Mode = BindingMode.OneWay;
MyTextBlock.SetBinding(TextBlock.TextProperty, myDataBinding);
Same leaky code can be also written in XAML:
Fix/Workaround:
There are few of approaches, the easiest one is simply to clear the binding when the windows is about to close.
e.g.:
BindingOperations.ClearBinding(MyTextBlock, TextBlock.TextProperty);
Other approach is to set the mode of the data binding to OneTime. See the kb article for other ideas.
Useful link:
Avoiding a WPF memory leak with DataBinding