Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug "Safe handle has been closed" error

Code which I have inherited keeps crashing out rather powerfully with the following error (not changed at all):

System.ObjectDisposedException: Safe handle has been closed
   at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(
          SafeFileHandle hFile, NativeOverlapped* lpOverlapped, 
          Int32& lpNumberOfBytesTransferred, Boolean bWait)
   at System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.
          ExecuteCodeWithGuaranteedCleanup(
          TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(
          ExecutionContext executionContext, ContextCallback callback, 
          Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
          ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

This is only being caught as the previous developers added a AppDomain.UnhandledException Event.

If I remove it, the application just crashes out with a Dr Watson message (send feedback etc...) and not the usual .NET dialog (with the continue option and stack trace).

I have checked and it is not related to Thread.Abort

How do I go about trying to find the cause of this issue, as it appears, from the stack trace, not to be in code of the application?

like image 484
Robert MacLean Avatar asked Oct 26 '09 14:10

Robert MacLean


1 Answers

From the fact that System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent() and Microsoft.Win32.UnsafeNativeMethods are referred to I would hazard that you have a COM component that has internal threads accessing a port e.g. for serial or TCP/IP data.

It would look like the thread throws an exception during it's start sequence. Possibly it is trying to access an unavailable or nonexistant port. This fails and the exception is not handled and thus propogates back through the code.

Trying logging more information from the UnhandledException Event in order to get an idea of where this may start from.

like image 102
ChrisBD Avatar answered Oct 12 '22 00:10

ChrisBD