views:

56

answers:

2

I wonder if I can remove all added event handlers in all the child forms from the one base form in the Closing method. (VB.NET; .NET 2.0)

Background: In a project I analyze a memory problem. I verified with the memory profile (see related question) and find out that some forms are not collected by the GC, probably because of the EventHandler non-removed references.

In the project all the forms inherits from the BaseForm. Now I search I way to remove in the OnClosed method foreach Control all the EventHandlers to/from the ChildForms.

Is that possible?

+1  A: 

Well, you could use Reflection to find all the events. But I think there's no way to enumerate the handlers assigned to an event. There're only add and remove accessors for event properties.

Michael Stoll
+2  A: 

No, Windows Forms makes this explicitly very difficult to do. It uses the add and remove accessors of the event, storing the event handler delegate in a list. The only documented way to remove the handler from that list is to use RemoveHandler, providing the exact same AddressOf argument. Poking the list is technically possible with Reflection, you'll need to first find the secret "cookie" that's used to identify the event in the list. You'll need to use Reflector or the Reference Source to find the name of the cookie.

Review this forum post for a possible reason why MenuItem causes leaks.

Hans Passant