Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Potential memory leaks when object instances die

$
0
0

Hello,

I am working on a pub/sub event broker of sorts. Not based on event-aggregator, but rather, this one operates on actual language level events (publishers), and wires up event handlers (subscribers) via reflection.

How to: Hook Up a Delegate Using Reflection

Hooking up the actual delegate using reflection is the first leg of that journey. So far so good. I am unit testing and mixing and matching 1-3 pubs/subs, and the connections and events are all happening as I expect.

The next leg on the journey might be to consider how to undo the registration process, in a couple of ways. First, to unregister the pubs and/or subs. But more importantly, how to guard against memory leaks connecting handlers with events.

It's plainly obvious to see that a live delegate is being injected into the mix when an object subscriber (handler) makes its presence felt on the object publisher (event).

One plausible solution is to use the WeakReference. On the surface seems like a valid approach. However, once the event handler(s) are connected, how to detect whether participating instances (and therefore their handlers) are still alive and not being kept alive by their handler(s).

Edit: The challenging part being, either side of the pub/sub could be put in a position to want to die off through the Garbage Collector, but for the event broker handler bookkeeping holding the (currently, strong) references.

Thank you...



Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>