Dabblernl
Dabblernl

Reputation: 16101

Resources to read about Databinding causing memory leaks in WPF?

My wpf app is eating memory every time the ItemsSource of a TreeView is replaced (I simply construct a new object and assign it to the bound property). Furthermore the memory hoarding only occurs when I use a DataTemplate that is used in other ItemControls too. When I remove the DataTemplate the Treeview reverts to displaying the ToString() method of the bound objects, but the memory loss stops.

Can anyone help me about how to troubleshoot this? Are there any resources on the internet about this topic?

I found a further link with useful information about WPF memory leaks.

Upvotes: 4

Views: 1508

Answers (2)

Christian Merat
Christian Merat

Reputation: 4304

There are quite a few binding scenarios that will cause a memory leak in WPF:

1) Binding to a property on an object that is neither a DependencyProperty or implements INotifyPropertyChanged in any other binding mode than OneTime may result in a memory leak since the first object will be retained in memory for property reference purposes.

2) There's a bug with a TreeView retaining indefinitely a reference to the first selected item. This is due to bug in the CreateUntargetedBindingExpression method that maintains a reference to the first item selected, even though it is an untargeted binding expression. This bug can be worked around with a bit of reflection to precompile the binding expression with a null binding item.

Upvotes: 3

Related Questions