I just need a little help to clarify how the Messenger class works with ICleanup in MVVM-Light. I am creating a WPF 4 application in VB.Net with Mvvm-Light v4.
I have a messenger that registers for a message sent from a NavigationService Class I created as follows:
This is the registration in the ViewModel named ClientListViewModel
''register for messages
Messenger.[Default].Register(Of INavigationService)(Me, "NavigationStart", False, AddressOf HandleParentChildNavigate)
This receives the NavigationService class and performs a check on it based on some other logic that is not relevant to this question.
The message is sent when a navigation event is triggered in my NavigationService class as follows
''Send message that navigation has been requested
Messenger.Default.Send(Of INavigationService)(Me, "NavigationStart")
This allows me to cancel the navigation event if my receiving class (in this case ClientListViewModel) has data validation errors, and it returns the focus to the record with errors. This all works great.
My question is, where and how do I unregister the message. I know that I need to in order to avoid memory leaks as I read on other posts. I have seen something like the following:
Public Overrides Sub CleanUp()
Messenger.Default.Unregister(Me)
End Sub
This cleanup is in the same viewmodel (CientListViewModel) the receives the message.
So I have three questions:
-
When should I call this cleanup method
-
Is ther a way with the ViewModelLocator to unregister all message recipients when the application is closed down?
-
This question is less relevant but I would appreciate some help too, How do I tell if I am getting “memory leaks” as a result of unregistered message recipients?
Thanks for your time
When viewmodels should be “cleaned up” depends on your application and viewmodels usage. For example, I work on application with tabbed interface. When user closes tab application calls cleanup on viewmodel representing that tab (which itself goes through its viewmodels and calls cleanup on them as well). So the general rule – as soon as you don’t need viewmodel anymore – you should cleanup it (closing child window, tab, etc.) As for the other questions:
2) It is indeed doesn’t matter on application closing if you cleanup your viewmodels. As on closing all memory will be freed up and you don’t get memory leaks 🙂
3) You should check application memory usage. In our application we had serious problems (and actually still have but not that big) with memory leaks. We determined that we could have leaks by memory tracking: opened/closed many tabs, called
GC.Collect()– but memory usage didn’t go down. We started to track memory leaks with WinDbg and found literally lots of places where we didn’t unregistered recipients from Messenger. Also, we’re using and old version of MVVM Light which is bound to CommandManager so we had problems with RelayCommands as well.Moral is – you should think about cleaning up resources during programming as later it can be painfull to find and fix it.