代码之家  ›  专栏  ›  技术社区  ›  Robert MacLean

如何调试“安全句柄已关闭”错误

  •  11
  • Robert MacLean  · 技术社区  · 15 年前

    我继承的代码不断崩溃,并出现以下错误(根本没有更改):

    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()
    

    这只会在以前的开发人员添加 AppDomain.UnhandledException Event .

    如果我删除它,应用程序就会崩溃,显示一条Dr Watson消息(发送反馈等),而不是通常的.NET对话框(带有Continue选项和堆栈跟踪)。

    我查过了,它与 Thread.Abort

    我该如何从堆栈跟踪中寻找这个问题的原因,而不是在应用程序的代码中?

    1 回复  |  直到 9 年前
        1
  •  9
  •   ChrisBD    15 年前

    由于引用了system.io.ports.serialstream.eventloprunner.waitforcommevent()和microsoft.win32.unsafenativemethods,我认为您的COM组件具有访问端口的内部线程,例如用于串行或TCP/IP数据。

    看起来线程在其开始序列中抛出了一个异常。它可能试图访问一个不可用或不存在的端口。这会失败,并且不会处理异常,因此会通过代码返回。

    尝试从UnhandledException事件中记录更多信息,以便了解可能从何处开始。