InfoQ

News

Serious Memory Leaks Plague WPF

Posted by Jonathan Allen on Oct 14, 2009

Community
.NET
Topics
Rich Client / Desktop
Tags
Memory Leaks ,
WPF

WPF, also known as Windows Presentation Foundation, represents the future of UI on the Windows platform. And if Microsoft has its way, its younger sibling Silerlight will take over the web and mobile markets. But like any new technology, it has some issues to work through including some rather serious memory leaks.

There are many memory leaks to discuss, so bare with us. The first one has been known for a while, but its cause is non-obvious to most developers, even ones reasonably familiar with WPF. The conditions needed to cause it follow:

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

This issue is described in detail in KB 938416.

Next up is a nasty bug that is triggered when you replace one data-bound collection with another. Ayende Rahien has a block of code that reproduces the issue. For an explanation, we turn to Mike Brown:

Digging in I discovered that the Binding System doesn't deregister the listener for the "Name" property when you notify that Data has changed.

It's definitely a bug and it has to do with the way the binding system looks at the binding in question. When you notify that Data has changed, rather than deregistering the existing bindings further down the tree (in this case the Name binding on your Textblock) and reusing the elements it appears that an entirely new set of elements are created. Unfortunately, the Textblock never gets the opportunity to deregister its binding.

Now if you made Data an observable collection (necessitates making your anonymous type into a full class) and made the collection raise a CollectionChanged event (e.g. Data[0]=Data[0]), everything works just fine.

This next group of memory leaks come to us from a Microsoft blogger going by the handle jgoldb.

  • CMilChannel leaks if initial HWND is destroyed on XP.
  • ShutdownListener leaked for each thread using Binding
  • Create and Destroy WriteableBitmap on XP in HW
  • SW Viewport 3D w/ VisualBrush, WB, etc. leaks on XP

In addition to these leaks, he lists some other common developer mistakes that lead to memory leaks as well as some past WPF issues that have been since corrected.

Memory leak or memory leak? by Jim Leonardo Posted Oct 14, 2009 5:39 PM
Re: Memory leak or memory leak? by Werner Schuster Posted Oct 15, 2009 8:43 AM
so true by chris donnan Posted Oct 16, 2009 2:45 AM
  1. Back to top

    Memory leak or memory leak?

    Oct 14, 2009 5:39 PM by Jim Leonardo

    I was conducting an interview for a developer position along with one of the other team leads and was reminded of how different developers mean different things by "memory leak".
    To an 'old school' dev, a memory leak usually means memory that's still allocated after program execution completes and not released back to the OS.
    To a Java/.Net developer, we usually mean something that is 'mysteriously' not being garbage collected.
    Usually, in a GC'ed language, failure to collect is traceable to the developer in the long run. However, it requires the dev understanding nuances to anticipate the problem. Examples include events and handles to unmanaged resources in the .net world. At any rate, this kind of leak usually goes away with application shut down. Quite unlike an old school lead, that is.

  2. Back to top

    Re: Memory leak or memory leak?

    Oct 15, 2009 8:43 AM by Werner Schuster

    I'm not sure about your old school dev memory leak - all memory in an address space is returned to the OS after a program is terminated, so there's not much chance for a leak there - at least in modern OSes released in the past decade [At this someone'll point out a reference to an arcane bug or behavior in some OS that _does_ allow that].

    I guess there might be some chance for a resource leak, where in some funky situation a program acquires a resource, then dies but doesn't manage to return it (and the resource is not one that automatically gets released after the demise of the process). But I wouldn't quite call these memory leaks.


    If there's a memory leak in a non-GC environment, it's simply a case of a malloc (or one of its pals) that's not matched by a free.

    In GC environments I find memory leaks are usually not caused by bad book keeping of memory blocks but by conceptual errors at a higher level; eg. adding listeners to models or GUI elements but not removing them, which is bad if the model or GUI lives longer than the developer expected.

  3. Back to top

    so true

    Oct 16, 2009 2:45 AM by chris donnan

    The guy who sits next to me said "hey Chris - it seems like you just posted this on Infoq!". I have been working on a WPF UI for some months now and continue to find WPF leaks galore. The bottom line is that it does not do what you expect - you must know when things are not taken care of by WPF. The APIs are therefore 'leaky' as well.

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.