I love the MVVM Light's Messenger and its flexibility, however I'm experiencing memory leaks when I forget to explicitly unregister the recipients (in Silverlight 4).
The cause is explained here, but I'm fine with it as I believe it's a good practice to explicitly unregister the recipients anyways rather than relying on the Messenger's use of weak references. The problem is that is easier said than done.
ViewModels are easy: you usually have full control over their lifecycle and can just
Cleanup()
them when they are not needed anymore.Views on the other hand are trickier because they are instantiated and destroyed via DataTemplates. For ex. you can think of an
ItemsControl
withMyView
as DataTemplate, bound to anObservableCollection<MyViewModel>
. TheMyView
controls are created/collected by the binding engine and you have no good way to manually call Cleanup() on them.
I have a solution in mind but would like to know if it's a decent pattern or there are better alternatives. The idea is to send a specific message from the ViewModel to tell the associated View(s) to dispose:
public class MyViewModel : ViewModelBase
{
...
public override void Cleanup()
{
// unregisters its own messages, so that we risk no leak
Messenger.Default.Unregister<...>(this);
// sends a message telling that this ViewModel is being cleaned
Messenger.Default.Send(new ViewModelDisposingMessage(this));
base.Cleanup();
}
}
public class MyView : UserControl, ICleanup
{
public MyView()
{
// registers to messages it actually needs
Messenger.Default.Register<...>(this, DoSomething);
// registers to the ViewModelDisposing message
Messenger.Default.Register<ViewModelDisposingMessage>(this, m =>
{
if (m.SenderViewModel == this.DataContext)
this.Cleanup();
});
}
public void Cleanup()
{
Messenger.Default.Unregister<...>(this);
Messenger.Default.Unregister<ViewModelDisposingMessage>(this);
}
}
So when you call Cleanup() on a viewModel all the views that use it as DataContext will exeute their local Cleanup() as well.
What do you think? Am I missing something obvious?