I just finished desktop apps written in WPF and c# using MVVM pattern. In this app I used Delegate Command implementation to wrap the ICommands properties exposed in my ModelVie
After reading this post, I then came across a web page that had some relating information. It is a page on CodePlex called Memory Leak caused by DelegateCommand.CanExecuteChanged Event.
Reported by : huetter
Updated by : dschenkelman
When profiling my application I noticed that plenty of EventHandlers had never been deregistered from DelegateCommand's CanExecuteChanged-Event. So those EventHandlers were never been garbage-collector, which caused a severe memory leak.
As registering CanExecuteChanged-EventHandles is done outside application code scope I had expected them to be deregistered automatically as well. At this point I thought this might as well be a ThirdParty WPF control issue, but digging further I read a blog post stating that "WPF expects the ICommand.CanExecuteChanged-Event to apply WeakReferences for EventHandlers". I had a look into RoutedCommand, and noticed it uses WeakReferences as well.
I adapted DelegateCommand to use an implementation similar to RoutedCommand's CanExecuteChanged-Event, and the memory leak was gone. The same is true for CompositeCommand.
Closed Nov 3, 2009 at 6:28 PM by This issue was fixed in the Prism-v2.1 release, so the Workitem is closed now. Prism 2.1 can be downloaded from here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=387c7a59-b217-4318-ad1b-cbc2ea453f40&displaylang=en
In your case, what contains a reference to what?
DelegateCommand
contains a reference to ViewModel
- its execute
and canExecute
properties contain references to a methods of the ViewModel
instance.
ViewModel
contains a reference to DelegateCommand
- its PrintCommand
property.
The view contains any number of references to the ViewModel
.
The CommandManager
contains a reference to DelegateCommand
in its RequerySuggested
event.
That last reference is a special case: CommandManager
uses a WeakReference
in its RequerySuggested
event, so despite the fact that DelegateCommand
registers for that event, it can still be garbage-collected.
Given all this, you shouldn't be having a problem. If the view gets disposed, neither the ViewModel
nor the DelegateCommand
should be reachable.
You say you've profiled the application and DelegateCommand
is holding a reference to ViewModel
. It seems to me that the logical next question should be: what's holding a reference to DelegateCommand
? It shouldn't be CommandManager
. Do you have something else in your application that's referencing your commands?
I think that in this code there is a circular reference which is causing the ViewModel to never be garbage collected.
I know this is an old question, but I will point out that some implementations of DelegateCommand or RelayCommand hold a WeakReference to the action. Your use of the DelegateCommand here is typical, but unfortunately will cause memory leaks with this implementation because when the ViewModel's method is passed into the DelegateCommand's constructor, a reference to the class containing that method is automatically captured by the delegate.
If you implemented IDispose on your ViewModel and cleared the references to the DelegateCommands explicitly in Dispose, then you could continue to use this implementation. Your view that's constructing your ViewModel would also have to Dipose of it, however.