When an object references another object, he prevent garbage collector to reclaim referenced objects, this fact is known by every developer and we need to pay specific attention when dealing with singleton objects.
One of the most common risk is having a Singleton reference other objects and thus keeping them alive until the application consumes all available memory; I have a primitive IBroker interface implemented by a ConcreteBroker class that have a singleton lifecycle; since all ViewModels register themselves into the broker to receive messages, we can end with all ViewModels not being released from memory because referenced from the Broker singleton instance.
Each VM correctly deregister all registered handler from the broker, but I wish to be able to improve the design, avoding the broker to keep hard reference to called object using a WeakReference. The broker have a couple of way to register message, the first one accepts a token and a Action<Message<T>> delegate and the token is used to deregister all messages associated with the token. With this interface I can pass the instance of the ViewModel as token, and since the ViewModelBase implements the IDisposable interface, I deregister all messages related to this, thus avoiding hard reference problem.
This is quite good, but I want to prevent the broker from keeping hard references to objects, so I created a test class to verify that the broker prevents objects from being disposed.
This class accepts a reference to a broker in the constructor, and simply increment a shared variable when it got finalized. Now we can write a test.
Such a test is not probably a perfect UnitTest, because it is not deterministic (it is based on assumpion on GarbageCollection); it simply creates a Finalizable instance passing the broker as a reference, then forces garbage collection, and finally checks if the instance of the Finalizable class was finalized. Clearly this test fails.
In such a scenario a possible solution is the WeakReference class, available since version 1.1 of the framework. This is needed because the Broker stores delegate to member function of object and thus it is preventing objects from being disposed. Now I changed the broker to use WeakReference, since the broker stores a Registration object for each registered message, I simply changed the registration object in this way.
Now the above test passes, because the Broker does not store hard references to the object, but is this really a good solution? Probably not, and we will see in another post why this solution is wrong.