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

  • 相关阅读:
    sqlserver 表操作 SQL篇
    C#知识点汇总
    DDL
    sqlserver2008简介
    面向对象继承
    IO文件流
    【帅刺猬课堂】Winform中使用WPF的UserControl
    KS Gantt甘特图控件通过递归加载无限层级的数据
    Office 每次打开需要重新配置的问题修复方法
    扩展方法
  • 原文地址:https://www.cnblogs.com/liuslayer/p/8930621.html
Copyright © 2011-2022 走看看