5

I'm looking into getting started using MVVM Light, and I came across the "new" ICleanup interface. I was just wondering when would you cleanup the VM...when you navigate away from the page?

Also, I see there is a Main Cleanup in the ViewModelLocator which should cleanup all the VM's...when would this be used?

Thanks very much

Regards, Mauro

1 Answer 1

8

The advantage of the ICleanup interface over the IDispose implementation (that was previously there) is - as Laurent states it - that you can call it more frequently and without marking the VM as disposed. This means, you should call ICleanup.Cleanup whenever you want/need to unregister the message handling for your VM. Obviously in this case you need to have a method that register all message handlers again when you need them later.

Personally, I'm more a friend of the IDispose way of dealing with VM cleanup, especially as I are inclined to IOC containers. But, I can see Laurent's case and implementing IDisposable calling Cleanup on a VM is no trickery.

In general, the point in time when you dispose/cleanup a VM depends on how it is instantiated and the lifespan of the object. These decisions are governed by the desing of your application and there is no clear guidance to when you should do it. But keep in mind that it has to be done whenever you registered a message handler within your view model - in other cases it is not strictly needed.

And while talking about message handlers, don't forget to de-register them in your views as well when you have registered a message handler there (see this post). - On second thoughts, I'll put the code here to make it clear and for future reference:

In your view's constructor in the code behind file add the following code to ensure that the registered message handlers are released when the view is unloaded:

public MyView() {
    this.Unloaded += (o, e) => { Messenger.Unregister(this); }
}
5
  • thanks for the reply. I now better understand the whole concept of cleaning up after the messenger
    – Mauro
    Commented Aug 12, 2011 at 11:37
  • I thought fact that the messenger uses WeakReference was so that unregistering was not necessary, and that one would unregister for other reasons than cleanup. Commented Feb 2, 2012 at 14:16
  • 2
    @RalphShillington: It should do it but it doesn't. It is a known problem. When registering a handler it is the function that causes the reference to be held although it shouldn't. The reason for this is that for anonymous and static funcitions weak references to the functions do not work. Laurent, some other people, and me looked into this and the best advice is ... deregister! Commented Feb 2, 2012 at 14:46
  • Just find a problem with using Unloaded for this kind of scenario. What seems to me is that if my view is hosted in a TabItem and user switches to another TabItem, the TabItem's Unloaded will be raised.
    – 000
    Commented Feb 7, 2012 at 3:35
  • Yes, this can be a problem. However, it also depends n the design of your application. Generally, I would not hold on to a view instance once it is no longer needed. If you need to hold on to the window, you might consider an alternative initialization strategy, e.g. a method hat is called before the window is shown. Commented Feb 19, 2012 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.