BT

InfoQ Homepage News Three Hotfixes for WPF Memory Leaks

Three Hotfixes for WPF Memory Leaks

Bookmarks

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

Adoption
Style

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.

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

Community comments

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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.