zoukankan      html  css  js  c++  java
  • C# 串口 已关闭 Safe handle

    今天在使用我的通信框架,创建基于串口通信的客户端时,出现这样一个BUG:

    [csharp] view plain copy
     
    1. 未处理 System.ObjectDisposedException  
    2.   Message=已关闭 Safe handle  
    3.   Source=mscorlib  
    4.   ObjectName=""  
    5.   StackTrace:  
    6.        在 System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)  
    7.        在 System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)  
    8.        在 Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFile, NativeOverlapped* lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean bWait)  
    9.        在 System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()  
    10.        在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)  
    11.        在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)  
    12.        在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  
    13.        在 System.Threading.ThreadHelper.ThreadStart()  
    14.   InnerException:   


    当这个BUG出现时,程序直接崩溃,无法跟踪到错误代码位置。

    经过一番测试,查找问题。发现在一个函数内利用SerialPort创建了串口通信实例后,没有将该实例放入任何其他容器中进行缓存,从而导致了上述崩溃BUG的产生。

    后来修改代码,将该实例放入了一个全局变量List<通信客户端>集合中,该BUG得以解决。

    原文地址:https://blog.csdn.net/yeqi3000/article/details/41983517

  • 相关阅读:
    Matlab将字符串改成变量名-eval
    Workbench-材料库
    UG-装配
    FreeCAD将Macro自定义到工具栏
    FreeCAD加载ui文件显示于组合浏览器
    字符生成线条字-xdd1997
    ANSYS求解器
    常用物理量及其单位以及材料信息
    Appium | UiAutomator exited unexpectedly with code 0, signal null
    【转】windows改变Sublime选中背景颜色
  • 原文地址:https://www.cnblogs.com/liuslayer/p/8930621.html
Copyright © 2011-2022 走看看