Category Archives: Events

WPF possible memory leak with EventManager.RegisterClassHandler

In the project I’m currently working on, I’m using the DataGrid from the WPF toolkit (that will be part of .Net 4.0 btw). Because I needed to tweak its behavior for copy/paste operations, I created a new class, PropertyDataGrid that inherits the DataGrid of the toolkit (you’ll notice that this time I used inheritance and not simply an attached behavior :p). In my DataGrid, I needed to register an event handler to be notified when a key is pressed in a DataGridCell. They are several ways to accomplish that:

  • If you’re tied to a XAML file (with a UserControl for instance) you can use an EventSetter
  • If you’re using a class that derives from ItemsControl you can override the GetContainerForItemOverride and IsItemItsOwnContainerOverride method to make the control uses your sub class (for example a CustomListBox that uses CustomListBoxItem) and do the job in the sub class (CustomListBoxItem)

Because those 2 approaches weren’t good in our case, we used the EventManager class that is very useful for this kind of operations. In one line of code, you can register an EventHandler for any RoutedEvent of any class:

public PropertyDataGrid()
{
    EventManager.RegisterClassHandler(
        typeof(DataGridCell), 
        PreviewKeyDownEvent, 
        new KeyEventHandler(this.OnCellPreviewKeyDown));
}

I wrote this code a couple of months ago. Then, a tester explained me that the memory used by our window was never reclaimed. Because the user can open several windows, the memory of the application was growing very quickly.

After spending a couple of hours tracking this memory leak, I found out that it was the call to RegisterClassHandler that was causing the leak. Using a non-static method (OnCellPreviewKeyDown) was causing the EventManager to keep a reference to the PropertyDataGrid control. This strong reference prevented the control for being garbage collected.

The solution was quick and easy; we just make the method static and initialize the handler in the static constructor. This way the delegate does not have a reference to the instance of the control, and can be kept by the EventManager without causing the leak:

static PropertyDataGrid()
{
    EventManager.RegisterClassHandler(
        typeof(DataGridCell), 
        PreviewKeyDownEvent, 
        new KeyEventHandler(OnCellPreviewKeyDown));
}

And voila ! We move from a solution eating always more memory to something reasonable:

before
after

Even using tools such a Memory Profiler, it was difficult to find the source of the leak, but at least, now, I’ll use carefully the EventManager class.

Note that of course you can have memory leak problems too if you don’t unregister your event handlers properly. If you want more information about that you can check out this article on CodeProject.

The future of WPF at Mix09

mix09

MIX09 is now over and the good news is that we can watch all sessions that have been recorded online !

I didn’t have time yet to watch all sessions I’m interested in, but I already saw “What”s new in WPF 4” video (available here).

Here are the important points of this session regarding the future of WPF:

  • Lot of new things are coming in WPF4: Text clarity improved, Multi-touch, Windows7 integration, Ribbon control, Focus management improvements, Visual State Manager, Client profile, Themes, Chart controls and a lot of bug fixes
  • WPF4 will come together with .Net4 which brings its own set of cool new stuff: Dynamic Language, MEF, F#, parallel library
  • WPF will be the best choise for RAD under Windows7: Multi-touch, Taskbar integration, Ribbon, Common dialogs, File explorer customization…
  • Multi-touch support: UIElement changes to manage touch related events, touch support is added for some controls (ScrollViewer)
  • New composition API: developers can control graphical elements cached in video memory
  • Controls that are currently available in the WPF toolkit will be integrated into the platform (DataGrid, DatePicker…) – moreover an update of the toolkit has just been released
  • Developers tools are improved: VS2010 and Blend3 helps the usage of WPF (databinding support…)
  • .Net4 will come with a new XAML parser: faster, extensible, public API to manage BAML format
  • .Net4 XAML language has new XAML features: support for generics, better references by name

I hope will see a CTP soon so that we’ll start playing with those new features 🙂

First look at Expression Blend 3 !

mix09

The keynote at MIX09 is almost over and Expression Blend 3 has been announced !

Go ahead and grab this new version from the Microsoft Expression web site.

A video is also already available on Channel9, where Unni Ravindranathan takes on a tour to demonstrate the new features of Blend 3. I just finished watching the show and here are my notes:

  • Blend3 supports Silverlight3 but there is more than that: lot of new functionalities to improve the user experience and the productivity
  • Goal: be more creative inside the tool
  • In Blend2, it was sort of complicated to select objects, particularly with nested containers. With Blend3 it’s a lot easier, we no longer have to navigate inside the objects explorer. Just click and you’re done
  • New features to manipulate the gradients : we can manipulate the gradient stops directly from the artboard
  • Supports for shaders : the assets panel now contains a “shader” category – drag’n’drop on the artboard and you’re done
  • Supports for projections in Silverlight3, new category from the property grid to edit them
  • Import from Photoshot and Illustrator functionality !
  • XAML Intellisense (oh my god !)
  • Team Foundation Server support
  • Sample data source generation inside Blend3 (Blend is able to generate sample data such as texts and images)
  • New features: behaviours that can be drag’n’dropped from the assets panel on the artboard to attach a piece of code to a visual element (could be used to start an animation for example)
  • Easing functionality when working with animation: define elastic, cubic, exponential animations (I think designers are going to have some fun)
  • New extensibility points to the Editor: custom adorners, custom panels, extend property grid, etc.

Here are some screenshots I just took running the preview on my machine. I think Blend3 is going a major step forward in the Expression Studio 3. Don’t wait a second to play with it !

welcome

Welcome to Blend3 !

assets

The new assets panel

animations

Easing functionality

intellisense4

XAML Intellisense !

kick it on DotNetKicks.com