Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Three Hotfixes for WPF Memory Leaks

Three Hotfixes for WPF Memory Leaks

This item in japanese

Windows Presentation Foundation is quickly becoming well known for the ease in which memory leaks are introduced. For example, it heavily relies on weak references for its data-binding technology. Like concurrent multi-threading, weak references seem easy enough but are in fact surprisingly difficult to use correctly. Even when the actual objects are being released in a timely manner, WPF’s arrays of weak references hang around and under stress become the memory leak.

This is especially apparent when using a large number of short-lived elements. KB981107 addresses this in three areas. First, WPF can leak application-level resources for styles and templates if the control that references the resource never actually uses it. Jossef Goldberg elaborates,

An example:  App resources define   <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">20</sys:Double>     This creates a resource that's used by any control that can display a horizontal scrollbar.  Creating a large number of such controls that never actually display their scrollbar will encounter the leak.

Another situation covered by this hotfix is when lots of short-lived elements have properties that are data-bound to a dependency property on a long-lived object. The weak reference from the long-lived object to the short-lived object is kept long after the short-lived object has been destroyed.

Unfortunately the KB981107 hotfix isn’t available for .NET 4.0 yet and Microsoft hasn’t even decided if it intends to release one. Two other hotfixes, KB967634 and KB967328 were shipped as part of .NET 4.0. so anyone on that platform doesn’t need to worry about them.

Rate this Article