Application.Idle, remove event handler?

C

Chien Lau

I have a form class that acts as a top level window. The user can open
and close any number of these windows throughout the lifetime of the
application. In the OnLoad, I have:

Application.Idle+=new EventHandler(Application_Idle);

....and in the Dispose() override, I have:

Application.Idle-=new EventHandler(Application_Idle);

First question: Is the call in Dispose() necessary? I suspect it is. If
it weren't called, the Application object would continue to reference a
pointer to a disposed form. This wouldn't matter if the application were
about to shut down, but in a situation where the user continually opened and
closed these windows throughout the day/week, a memory leak would occur. Is
this correct?

I'm asking this because of a bug that's been in the WinForms framework
for sometime now. It occurs when an application closes. It only occurs in
debug mode. The symptom is as follows: The garbage collector thread crashes
with an unhandled exception (NullReferenceException). It turns out that
removing the idle event deregistration code in Dispose() fixes the error...
but it got me to wondering: Is it really necessary to perform this
deregistration at all?

Thanks...
 
E

Etienne Boucher

I can't anwser the other questions, but I would do this during the closing
of the Window if it was possible.

Etienne Boucher
 
C

Chien Lau

I had the same thought. Moving the deregistration to OnClosing() or
prior to the Close() call yields the same bad result, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top