I have a class, Plotter, which has a static event (declared public static event MouseEventHandler MultiCursorMouseMove for those wondering how I have a static event).
It emulates a graphical plot and our current application displays about 30-40 of them. The purpose of the event is to draw a cursor across all displayed graphs to indicate the value for the graph at the time the mouse cursor is pointing at.
The problem I’m running into is that, because the Plotter instances have this static event, they refuse to die. As best I can tell looking at them in a memory profiler, every instance of a Plotter (as well as the Form which contains them) is being held in memory by an EventHandler tied to this MultiCursorMouseMove event. I have coded set up which runs the Dispose method on each of the Plotter instances, which includes removing the delegate from MultiCursorMouseMove, but it doesn’t seem to be doing any good. According to what I can see via the debugger, Plotter.MultiCursorMouseMove registers as null, but these instances still aren’t getting collected and, best as I can tell via the profiler, all of the instances continue to exist due to MultiCursorMouseMove.
So, the question I have is, is there any way to explicitly state “remove all delegates associated with this event”?
I figure that way I’ll be sure I got rid of all of them, not to mention it will simplify things compared to my current code which, during the Dispose method, cycles through all of the controls on the tab and runs Dispose on all of the Plotter objects it finds.
To be perfectly open, I submitted this question last week but after saying it was solved, I found another issue. I tried editing the question, but it seems to have fallen out of public view.
Have you tried explicitly setting “MultiCursorMouseMove” to null?