In previous post I explained how I modified the Broker class to store WeakReference to Action<Message<T>> delegate to avoid the broker preventing garbage collection of the subscribers. This is a strong requirement, an object register an handler to the broker, but it does not want to share the same lifecycle of the broker because it is singleton. After this modification I have a strange bug, the program works, but after some minutes messages stops flowing, I scratched my head and then understand that this is normal, because my code that uses a WeakReference to an even is flawed. If you do not understand where the error is, think with care what is happening under the hood.
When an object registers a message listener to the broker, he passes a Action<Message<T>> object, and this object is stored by the broker with a WeakReference. The problem is that an Action<T> is a class that inherits from Multicast Delegate, thus is a real object, that as shown in Figure 1 keeps a reference to the target object that contains the method to be called.
Figure 1: The implementation of Delegate object taken from Reflector.
Now let’s look again at the code needed to register a message listener to the broker (line 3).
If you think carefully, this test creates a delegate, pass that delegate to the broker and the brokers stores a WeakReference on it to send message ( Figure 2 ).
Figure 2: The broker stores a WeakReference to delegate in a Registration object, but this is the only reference to the Delegate object.
The problem is that delegate is referenced only by a WeakDelegate and since no other object have a reference to it, it will be collected by the garbage collector after some time. To verify this problem we can change the code that register for message a little bit.
The only difference is that the caller stores a reference to the Registration object returned from the RegisterForMessage method, the broker creates a registration object and store a WeakReference on it ( Figure 3 ). This reference now keeps the delegate alive.
Figure 3: Now the object stores a reference to the registration object, and the broker a WeakReference to the same object
This is the original desired situation, until Object is alive, it takes a reference to the registration object, that keeps the delegate and the object alive. When the object is not references by any other root, except from the delegate, the garbage collector verify that the registration/delegate and object could be collected, because Broker has only a WeakReference to the Registration object.
This solution is not perfect, because if object forgot to keep references to Registration object, it will cease to receive message at the first passage of the garbage collector. A possible solution is using reflection to call instance methods.
There is no secret here, I pass target object and a method info to the constructor and then use Methodinfo.Invoke to call the handler method through reflection. This technique can be used only if the caller pass a call to an instance method, as done in this snippet.
As you can see I’m passing to RegisterForMessage both the object (this) and a lambda that calls the method, here is the implementation of this new version of RegisterForMessage.
This method accepts an Expression, and if the expression is a MethodCallExpression, we can use some Expression manipulation to extract the MethodInfo needed to create an instance of WeakRegistration class. If the Expression is not a methodCAllExpression defined on the same object passed as instance I can compile the expression and pass the compiled Action<Message<T>> to the standard ActionRegistration Object.
This is not a perfect solution, but if the caller restrict itself to register only instance method in the broker, we do not risk any leak.