mvvm light icleanup and messenger unregister in VB.NET

后端 未结 2 1986
北恋
北恋 2021-01-28 01:06

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

相关标签:
2条回答
  • 2021-01-28 01:56

    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.

    0 讨论(0)
  • 2021-01-28 02:07

    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:

    1. 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.

    2. This I do not know. But on app close it shouldn't matter (for managed objects, see below).

    3. 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.

    0 讨论(0)
提交回复
热议问题