.NET 2.0/3.5 中的串行端口内存泄漏
我在使用 SerialPort
类时遇到问题。
由于我们需要连接到多个设备,因此我们在通用列表中使用多个串行端口。
这就是我们的基本代码的样子:
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();
}
}
现在,单击按钮后,如果其中一个设备(在我们的例子中是手机)关闭或其电缆与 USB 端口断开连接,则会立即发生大量内存泄漏。
我注意到此处有一个类似的线程以及一些错误报告微软连接。
I'm having a problem with the SerialPort
class.
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.
I have noticed a similar thread here and a couple of bug reports in Microsoft Connect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定问题出在
SerialPort
上,而不是 USB 串行设备的驱动程序上? 我会尝试另一个测试来验证该问题:hyperterm
如果没有发生,则有这是一个特别针对
SerialPort
的错误。 如果再次发生这种情况,您至少会知道它与SerialPort
的实现无关。 问题可能出在 Windows 的 COM 端口代码或您正在使用的驱动程序中。 就我个人而言,我发现问题更有可能出在驱动程序中,但我很想知道 Windows 的串行端口是否存在一些未知的问题。我之前在连接/断开端口时使用过
SerialPort
,没有任何此类问题。您可以尝试的另一件事是调试 CLR 代码。 关于这个主题还有很多其他问题,所以应该很容易找到执行此操作的方法。 这应该可以让您进一步调试并准确查看
Open()
中发生内存泄漏的位置。 但请注意,由于它是系统串行端口的“简单”包装器,因此您可能很快就会看到它进入 P/Invoke 世界,并且可能看不到太多内容。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:hyperterm
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 withSerialPort
'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.I've used
SerialPort
before while connecting/disconnecting ports without any such problems.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.不确定问题是否这么简单,但是您是否正确处理
SerialPort
对象? 使用完每个实例后,您需要立即调用它们的Dispose
方法。Not sure the issue is this simple, but are you disposing your
SerialPort
objects correctly? You need to call theDispose
method on each instance as soon as you're finished with them.