Help with error message on Application close

J

Jim H

I sometimes get the following error from my Form's Dispose Method when the
application is closing:
-------------------------------------------------
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.windows.forms.dll
Additional information: Cannot call Dispose() while doing CreateHandle().
-------------------------------------------------
How do I find out what is calling CreateHandle()?

Here is the Dispose from the form:
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
m_Logger.StopLogging();
m_Logger.CloseConnection();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

I have a serial connection in m_Logger, but I close the connection and that
calls CloseHandle(). I don't call CreateHandle at all. I do call
CreateFile if I'm opening a serial connection, but not from Close(). When I
close the app I am not in the process of opening a connection.

Thanks,
jim
 
P

Phil Jenson

Jim,

Does the exception not give details as to exactly where this is occurring?
Try placing try/catch statement around suspect areas and place either break
points of Debug.Write statements to help home into the problem. For example,
is the exception raised within m_Logger.StopLogging() or
m_Logger.CloseConnection()?

Whilst this may not be the cause of your problem, all managed code should be
contained inside the "if (disposing)" block. You should make no attempt to
access managed code during a call from a finializer (ie when disposing is
false).

Phil...
 
J

Jim H

The application stops with a green line on base.Dispose(). Because the
error message says cannot call Dispose it must be in base.Dispose() or
Components.Dispose(). Neither of which are my code. It doesn't always
happen so I'll put a try...catch around each of those statements and see
which one catches it.

jim
 
P

Phil Jenson

Jim,

Having had a look at the code within the Form/Control classes I believe that
this exception can only occur as a result of Dispose being called from a
different thread to that used by the form (although it not possible for me
to know for certain). Are you using any asynchronous events or threading?
From where are you calling the Form.Dispose() method?

Could you post the stack trace from the exception.

Phil...
 
J

Jim H

The app does use asynchronous events and threading, but I never call
Form.Dispose(). It is called by the framework when I close the app.

jim
 
P

Phil Jenson

Jim,

Is Form.Dispose being called from you main UI thread. If you stop on a break
point you should see Form.Main() at the bottom of the call stack.

Phil...
 
J

Jim H

Yes the problem exists but intermittently. I'm still trying to see if I can
find a pattern that will cause it to fail.

Thanks,
jim
 
J

Jim H

Yes, I am still having that problem but it's very very intermittent. I'm
still trying to find a pattern so I can duplicate the problem more
regularly.

Jim
 
P

Phil jenson

Jim,

As mentioned before I suspect your problem is related to calling Dispose
from a thread other than the main UI thread. Is it possible to place a break
point on a catch statement within you Dispose method and check the call
stack and also the thread that it is called on? If the application quits
without allowing you to debug then try writing relevant information to a
trace file.

I appreciate you say you never call Dispose, so I assume you have shown the
form with myForm.Show() as this is the main (only?) way Dispose would be
called automatically, when the form is closed. It should be called on the
main message handling thread, in which case the stack trace/thread display
should confirm this.

Alternatively are you doing something on another thread at the point of
closing the form? Are you doing and GDI+ drawing for example or anything
which (indirectly) uses windows handles? Again look at the other threads
active to see (if possible) what they are doing when the exception occurs.

Or do you already no for a fact it is not thread related?

If its easily reproducible and your code is easy to transfer than I would be
willing to have a look if you want to send it. I offer not as an expert on
these issues but because sometimes it helps to have a fresh view on a
problem.

Hope this gives you some ideas.

Phil...
 

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