Race condition in usage of WeakReference


In MEFedMVVM.WPF/ViewModelLocator/ViewModelRepoitory.cs, there are a bunch of usages of the IsAlive property of a WeakReference. However, this property should only be used to check if the reference is not alive anymore, since a GC run can occur between checking the property and using the Target.

The correct usage is to get the value of the Target and checking if it is null. That way, there is a strong reference to the item and it cannot get garbage-collected.


DanielRose wrote Mar 6, 2014 at 12:16 PM

The same is the case for MEFedMVVM/Services/CommonServices/MessageToActionMap.cs, which uses WeakAction.CreateAction().