0

I have a device (USB->serial) and an application software (.NET 3.5) connected to the device COM port. There is a function which is able to reboot the device (done by sending a serial command to the device).

The problem is, ObjectDisposedException is thrown by System.dll when the device is rebooted. (randomly thrown & uncatchable even though I have wrapped the code inside try-and-catch block).

I have found some information online that this exception is due to SerialPort's internal Stream object getting disposed by the garbage collector when the physical port disappears.

I have implemented the coding i.e. GC.SuppressFinalize(SerialPort) and GC.SuppressFinalize(SerialPort.BaseStream) in connect function, and also GC.ReRegisterForFinalize in disconnect function as suggested here But it does not help to solve the ObjectDisposedException when USB is unplugged and removed from device manager.

Any suggestion to solve this issue?

  • `Any suggestion to solve this issue?` Are you able to show us a https://stackoverflow.com/help/mcve ? – mjwills Oct 30 '17 at 10:00
  • Serial ports are not plug-and-play devices so cannot know that the USB device driver suffered a heart attack and they are holding on to a junk handle. Same kind of problem as jerking the connector while the port is being used. And the primary reason that the "Safely remove hardware" tray icon exists. You **must** interlock this in your code/ui and Close() the serial port before you do this. – Hans Passant Oct 30 '17 at 11:41
  • Hi mjwills, The issue randomly occurs when I connect to the device COM port and physically unplug the USB cable. I only got the issue once so far in my own Windows 8 PC, but my teammate got the issue more frequent than me in his Windows 10 PC. Hi Hans Passant, do you mean that I need to manually disconnect from the serial port BEFORE physically unplug the USB cable? I have implemented disconnect the serial port when USB is removed. – user8783507 Oct 30 '17 at 23:43

0 Answers0