views:

1030

answers:

3

Hello,

I'm having a very big problem with the SerialPort class and I need some help to solve this.

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.

Please help us with a solution.

I have noticed a similar thread here and a couple of bug reports in Microsoft Connect. But right now I need an IMMEDIATE URGENT solution... Would really appreciate if someone helps. Thanks so much.

+1  A: 

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.

Noldorin
`Dispose()` is only there to allow for *explicit immediate* release of a resource. Every class that uses unmanaged resources also implements a finalizer, in case the object goes out of scope without having been disposed explicitly. A safety net if you want.
SealedSun
Yes, the GC will of course get around to disposing unmanaged resources used by the class anyway, but this may not happen for a while, especially if objects are in the global scope. This could easily lead to a memory leak.
Noldorin
+1  A: 

If you need an immediate and urgent solution, then, good as StackOverflow is, you need to call Microsoft Support and open a ticket.

John Saunders
+2  A: 

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:

  1. Start up hyperterm
  2. Connect to your problem device
  3. Check memory usage
  4. Disconnect in same way that causes problem in C#
  5. Check memory usage and compare

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.

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.

Erich Mirabal
Oh, and to comment on design, your on_click should not do the code, it should call a method that does the opening (factor out that functionality so it does not belong in the UI code).
Erich Mirabal