4

I am using MvvmLight 4.0/C# in my first WPF4 project and still learning MVVM/WPF ropes where I find many of my former windows forms skills ineffective.

Anyway, I have viewmodels that derive from viewmodelbase that Register for messages and likewise have views do the same (Register for messages) for VM/VM and VM/V communication. All mvvmlight resources on cleanup say that I should Unregister messages to avoid memory leaks.

So when I am done using the views, I just call Messenger.Default.Unregister(this) in the unload event of the view/window. And when I am done using a viewmodel, I just invoke viewmodelbase.Cleanup() on my viewmodel reference assuming that the base implementation would do the (blanket) unregistering.

I want to know if just invoking Cleanup() on the viewmodel is enough or do I have to override this method in each of my viewmodels and explicityly call Unregister from within each override. For now I create/dispose most of my viewmodels on adhoc basis (not using SimpleIOC/ServiceLocator) and am only interested in unregistering all messages in the cleanup.

I found following SO tags connected but still leaves me unanswered on my query over implications of simply invoking ViewModelBase.Cleanup() vs Unregistering by explicitly overriding the method in the derived viewmodel.

Unregister(this) unregisters this instance from everything?

When and where to unregister messenger with mvvmlight

Community
  • 1
  • 1
man.kar
  • 125
  • 2
  • 9
  • 1
    So I get to conclude that if I call Cleanup() on my derived viewmodels so that the ViewModelBase.Cleanup() is invoked, I can be assured that all my instances are completely unregistered from the Messenger and hence no chance of any memory leaks on this account. Thanks for all the help. – man.kar Dec 17 '13 at 02:30

2 Answers2

5

Just look at the code, it's open source after all.

    /// <summary>
    /// Unregisters this instance from the Messenger class.
    /// <para>To cleanup additional resources, override this method, clean
    /// up and then call base.Cleanup().</para>
    /// </summary>
    public virtual void Cleanup()
    {
        MessengerInstance.Unregister(this);
    }

So what the Cleanup method actually does it pretty clear. And in case you want other cleanups:

To cleanup additional resources, override this method, clean up and then call base.Cleanup()

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • what file is that in, I looked in ICleanup.cs and Viewmodelbase.cs and I can't find the Cleanup Void itself. Just the interface. – J King Dec 16 '13 at 19:33
0

No, cleanup will not auto unregister. You will have to override the method in each viewmodel and call Messenger.Default.Unregister(this). This will unregister the viewmodel from all events it has been registered for.

I was wrong. thanks ken2k. I found some other posts online that suggested it didn't. Can't seem to find them now. Anyway it looks like cleanup calls unregister for the viewmodel messages.

THis is from LB himself:

The intent of ICleanup is to provide a way to clean VMs (for example flushing their state to persistent storage, closing streams etc...)

from This post on SO

Community
  • 1
  • 1
J King
  • 4,108
  • 10
  • 53
  • 103
  • Thanks for comments. Kindly also clarify would it suffice if call Messenger.Default.Unregister(this) in my base viewmodel. All my viewmodels derive from a common IDisposable viewmodel (which in turn derives from MvvmLight ViewModelBase) and I would like to put this call to Unregister in the base Dispose method. – man.kar Dec 16 '13 at 19:06
  • looks like I was wrong, see the other answer. As per your other question: You could call the cleanup in your iDisposable and also look into the cleanup method of your viewmodel locator. You can cleanup/dispose from there as well. – J King Dec 16 '13 at 19:24