Tomer W
Tomer W

Reputation: 3433

Why did MVVMLight's RelayCommand use WeakAction

I noticed that GalaSoft.MvvmLight.CommandWpf.RelayCommand<T> using WeakAction<T>.

this design consideration was surprising to me and I did a little further investigation.
while browsing the web, I came across this blog post which strengthen my suspicion, but did not answer my original question as to "Why?"

Why would GalaSoft intentionally use WeakAction instead of plain Action allow the functions to get Garbage Collected?

Thank you.

Upvotes: 2

Views: 947

Answers (2)

mm8
mm8

Reputation: 169160

Why would GalaSoft intentionally use WeakAction instead of plain Action allow the functions to get Garbage Collected?

Most probably to prevent the commands(s) from keeping the owner(s) of the action(s) alive and cause memory leaks in an MvvmLight application.

Using a WeakAction<T>, the owner is still eligible for garbage collection despite the fact the command may be not as there is no strong reference between the command and the owner of the action.

Upvotes: 3

dhilmathy
dhilmathy

Reputation: 2868

This to create Weak Reference with the Action's owner. As that the owner Action can be garbage collected at any time.

Ref.

http://www.mvvmlight.net/help/SL5/html/aac8fdf1-873d-4e6b-ce31-af2dcf02d1d0.htm https://github.com/lbugnion/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(PCL)/Helpers/WeakAction.cs

Upvotes: -1

Related Questions