BT

Three Hotfixes for WPF Memory Leaks

by Jonathan Allen on Jun 17, 2010 |

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.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT