« Pre-sales "support"? | Main | Astaro Security Gateway and Microsoft Virtual Server --- I'm stuck. »

Memory Leak with Databinding in WPF

If you're using WPF and doing any databinding, make sure you read KB article at Microsoft carefully:

A memory leak may occur when you use data binding in Windows Presentation Foundation

A snippet from the KB article:

This issue occurs if the following conditions are true:

    • A data-binding path refers to property P of object X.
    • Object X contains a direct reference or an indirect reference to the target of the data-binding operation.
    • Property P is accessed through a PropertyDescriptor object instead of a DependencyProperty object or a PropertyInfo object.

 

In the following code example, the conditions for the leak are created.

<Label Name="MyLabel">
   <Stack Panel Name="MyStackPanel">
      <TextBlock Text="{Binding ElementName=MyStackPanel, 
Path=Children.Count}" /> </StackPanel> </Label>

 

Yes, it's really that simple! There are some work around methods, IF you happen to own the object you're binding to ... if not, you may need to create a facade/shim around the object.

Help support my web site by searching and buying through Amazon.com (in assocation with Amazon.com).