PolkaDance

Hi,

I have a memory leak problems that is resolved by either nullifying the datasource or unregistering the events of a Combobox. Nullifying the datasource might trigger events so I'd rather not use that solution. I could -= all the events of all my controls but what's I'd rather have is a way for the components to unregister their own events and since they don't know which ones were registered, I can't use the -= with explicite events.

the controls have a EventHandleList call Events but it doesn't really work like other collections (can't really loop on it).

Events.Dispose() method doesn't do the job either, the leak is still there so I presume that the events aren't really unregisted.

So, What I'd like to know is if there is a way to loop thru all the registered events to unregister them

I looked at the help for EventHandlerList since it is the type of Events property and it lists a public property named Item as being the delegate list but strangely, there is no such property in Events, not even hidden; obviously, the help is wrong or is it is there a way to expose that property

Thanks



Re: Windows Forms General Controls unregistering their own events

Peter Ritchie

Yes, unfortunately the type that implements Events does not implement GetEnumerator so you can't iterate the delegates (and keys) contained within it.

The easiest and best way to remove handlers for events in a form is to simply add the corresponding -= for the event. There's no have to removing an delegate from an event that hasn't been added.






Re: Windows Forms General Controls unregistering their own events

nobugz

Is this a databound form shown with ShowDialog() Check this thread. The OP wasn't happy with the answer but calling Dispose() after ShowDialog() should solve your problem.





Re: Windows Forms General Controls unregistering their own events

PolkaDance

Thanks for you answers but it isn't the dialogs that leanked but a non modal form. I tried to .Dispose on the Events property of the combobox but it didn't remove the memory leak while -= does so there is something that the .dispose doesn't do I guess.

As for having to use -=, it only works if you know the EventHandler and the Event it is bound to but what would be much more interesting than hard coding that would be to unbind events inside the component's dispose overriden method and -= would only work if you have the handle on each bound events but the Events property doesn't do the job.

I also tried to use this.GetType().GetEvents() on the component but I didn't find any event handlers in there.

One thing that works though is to use BindingSources components to bind the static data but the rework would be too expensive to do so I just put the datasouce to null in the dispose of the component.





Re: Windows Forms General Controls unregistering their own events

Peter Ritchie

A Form's or Control's Dispose method can't unhook all the events during disposal; because a class farther up in the hierarchy (Component) will raise a Disposed event.

Short or unrooting the delegates for the events by unreferencing the object in question (which would mean re-allocating a new one if you still need that type of object) and forcing a Garbage Collection, the only reliably way is to unsubscribe via -=.