.NET 2.0 / 3.5中的SerialPort内存泄漏

时间:2022-09-05 18:47:07

I'm having a problem with the SerialPort class.

我遇到了SerialPort类的问题。

We're using multiple serialports in a generic list since we need to connect to multiple devices.

我们在通用列表中使用多个串行端口,因为我们需要连接到多个设备。

This is what our basic code looks like:

这就是我们的基本代码:

List<SerialPort> ports = new List<SerialPort>();
private void button1_Click(object sender, EventArgs e)
{
    ports.Add(new SerialPort("COM6"));
    ports.Add(new SerialPort("COM7"));
    ports.Add(new SerialPort("COM8"));
    foreach (SerialPort port in ports)
    {
        port.Open();
    }
}

Now, after the button is clicked, if one of the devices (mobile phone in our case) is switched off or if its cable is disconnected from the USB port,there is an immediate massive memory leakage.

现在,在单击按钮后,如果其中一个设备(在我们的情况下是手机)关闭或者其电缆与USB端口断开连接,则会立即发生大量内存泄漏。

I have noticed a similar thread here and a couple of bug reports in Microsoft Connect.

我在这里注意到一个类似的线程和Microsoft Connect中的一些错误报告。

2 个解决方案

#1


Are you sure that the problem is with SerialPort and not the driver for the USB-Serial device? I would try another test to validate the issue:

你确定问题出在SerialPort而不是USB-Serial设备的驱动程序上吗?我会尝试另一个测试来验证问题:

  1. Start up hyperterm
  2. 启动hyperterm

  3. Connect to your problem device
  4. 连接到您的问题设备

  5. Check memory usage
  6. 检查内存使用情况

  7. Disconnect in same way that causes problem in C#
  8. 以导致C#问题的相同方式断开连接

  9. Check memory usage and compare
  10. 检查内存使用情况并进

If it does not happen, then there is a bug in particular to SerialPort. If it happens again, you would at least know that it has nothing to do with SerialPort's implementation. The problem might be in either the Window's COM Port code or in the driver you are using. Personally, I find it likelier that it the problem might be in the driver, but I'd love to know if there is some unknown issue with Window's serial ports.

如果没有发生,那么SerialPort就会出现一个特殊的错误。如果它再次发生,你至少会知道它与SerialPort的实现无关。问题可能出在Window的COM端口代码或您正在使用的驱动程序中。就我个人而言,我觉得问题可能在驱动程序中更有可能,但我很想知道Window的串口是否存在一些未知问题。

I've used SerialPort before while connecting/disconnecting ports without any such problems.

我在连接/断开端口之前使用过SerialPort而没有任何这样的问题。

Another thing you can try to is debug into the CLR's code. There are plenty of other SO questions on this topic, so it should be easy to find the method to do that. That should let you debug down a bit further and see exactly at which point in Open() the memory leak happens. Warning though, since it is a "simple" wrapper to the system's serial port, you might quickly see it go to P/Invoke world and will probably not get to see to much.

您可以尝试的另一件事是调试CLR的代码。关于这个主题还有很多其他的SO问题,所以应该很容易找到这样做的方法。这应该让你进一步调试,看看Open()中的哪一点发生了内存泄漏。但是警告,因为它是系统串口的“简单”包装器,你很快就会看到它进入P / Invoke世界,可能不会看到太多。

#2


Not sure the issue is this simple, but are you disposing your SerialPort objects correctly? You need to call the Dispose method on each instance as soon as you're finished with them.

不确定问题是这么简单,但是您是否正确处理了SerialPort对象?一旦完成,就需要在每个实例上调用Dispose方法。

#1


Are you sure that the problem is with SerialPort and not the driver for the USB-Serial device? I would try another test to validate the issue:

你确定问题出在SerialPort而不是USB-Serial设备的驱动程序上吗?我会尝试另一个测试来验证问题:

  1. Start up hyperterm
  2. 启动hyperterm

  3. Connect to your problem device
  4. 连接到您的问题设备

  5. Check memory usage
  6. 检查内存使用情况

  7. Disconnect in same way that causes problem in C#
  8. 以导致C#问题的相同方式断开连接

  9. Check memory usage and compare
  10. 检查内存使用情况并进

If it does not happen, then there is a bug in particular to SerialPort. If it happens again, you would at least know that it has nothing to do with SerialPort's implementation. The problem might be in either the Window's COM Port code or in the driver you are using. Personally, I find it likelier that it the problem might be in the driver, but I'd love to know if there is some unknown issue with Window's serial ports.

如果没有发生,那么SerialPort就会出现一个特殊的错误。如果它再次发生,你至少会知道它与SerialPort的实现无关。问题可能出在Window的COM端口代码或您正在使用的驱动程序中。就我个人而言,我觉得问题可能在驱动程序中更有可能,但我很想知道Window的串口是否存在一些未知问题。

I've used SerialPort before while connecting/disconnecting ports without any such problems.

我在连接/断开端口之前使用过SerialPort而没有任何这样的问题。

Another thing you can try to is debug into the CLR's code. There are plenty of other SO questions on this topic, so it should be easy to find the method to do that. That should let you debug down a bit further and see exactly at which point in Open() the memory leak happens. Warning though, since it is a "simple" wrapper to the system's serial port, you might quickly see it go to P/Invoke world and will probably not get to see to much.

您可以尝试的另一件事是调试CLR的代码。关于这个主题还有很多其他的SO问题,所以应该很容易找到这样做的方法。这应该让你进一步调试,看看Open()中的哪一点发生了内存泄漏。但是警告,因为它是系统串口的“简单”包装器,你很快就会看到它进入P / Invoke世界,可能不会看到太多。

#2


Not sure the issue is this simple, but are you disposing your SerialPort objects correctly? You need to call the Dispose method on each instance as soon as you're finished with them.

不确定问题是这么简单,但是您是否正确处理了SerialPort对象?一旦完成,就需要在每个实例上调用Dispose方法。