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.
I've not used the Messenger in MVVM-Light so I do not know if there is an inherent way to cleanup. Here are some general answers to these questions:
You should do your cleanup when you no longer need to know about the message or when no longer need your view model. If the Navigate is not canceled does your ViewModel care about the Navigate message any more (i.e. is it getting unloaded or is the view it supports going away)? If that is the case then you could unregister when you receive the Navigate command and have determined it is OK to navigate.
This I do not know. But on app close it shouldn't matter (for managed objects, see below).
You shouldn't need to worry about these memory leaks with managed objects when the app shuts down, any non-referenced objects will be trashed when the whole app domain is terminated. If you have unmanaged resources being referenced this is another story.