WAF and the potential for Memory Leaks

Mar 18, 2014 at 3:09 PM
I have a dumb question.

Many other MVVM frameworks have a lot of code, some of it quite complicated, to deal with the potential for Memory Leaks. Usually related to DelegateCommand.

But I see none of that here. Which made me a little nervous. But then I created a sample application, and didn't see any Memory Leaks. So I suppose it's all fine.

Are those other frameworks outdated, and there is no longer a need to worry about weak references to the target of Actions supplied in DelegateCommands? Or weak event handlers for CanExecuteChanged? Is that all "fixed" by .NET 4.5, or does it take more to leak than creating a View Model with DelegateCommands that reference a method on the ViewModel?

Thanks!
Mar 19, 2014 at 6:27 PM
I didn't realize how well the .NET garbage collector handles circular references. So this is not an issue. Now I have to wonder why other frameworks worry about keeping a strong reference to the Action.Target in their RelayCommands.
Coordinator
Mar 20, 2014 at 6:37 PM
You are right. WPF had a bug in the usage of the ICommand.CanExecuteChanged event. This results in a memory leak. However, it was fixed in .NET 4.5.

Older versions of WAF (< 3.0) had a DelegateCommand implementation that used weak references for this event. Since WAF 3.0 requires .NET 4.5 I have removed this workaround code.

See also:
http://connect.microsoft.com/VisualStudio/feedback/details/713892/wpf-icommand-canexecutechanged-memory-leak
Mar 20, 2014 at 8:11 PM
Awesome thank you. I'm glad I can use .NET 4.5.