I have a childwindow with an associated VM that gets created each time I ask the child window to open. when the childwindow opens, it registers a listener for an MVVM Light message. After I close the window, I’m pretty sure that I’m releasing all references to it, but I don’t actually call dispose because it does not implement IDisposeable.
When I instanciate another child window of the same type, and send it a different context, I know that I’m receiving the message from the previous instanciation of the VM… each time I use the window, more and more VM are listening, and the code repeats.
How can I be sure that my previous VM that registered to listen to a message, has actually been released and is no longer active. Is there a deterministic way to do this?
Whenever you register a message you should make sure that you unregister the message as well. To unregister you can use
Cleanupmethod on classes deriving fromViewModelBase. In other cases, e.g. a view, you should implement a method hat is called when the view is unloaded – e.g by trapping and handling the unloaded event on a control or view. In this method you then callMessenger.Unregister(EventTarget).This behaviour is a quirk in the current version of the toolkit, and Laurent is aware of it.