1

Closed

m_SerialPort never Disposed. Leads to OutOfMemoryException in OverlappedIoState.ctor.

description

Good Day!

Since m_SerialPort is never Disposed, disposing the SerialStreamPort leaves objects in Memory due to GCHandles. Do this a lot and you run out of memory.

Please find the patch attached.

Example of Exception with Stack:

System.OutOfMemoryException: Eine Ausnahme vom Typ "System.OutOfMemoryException" wurde ausgelöst.
bei RJCP.IO.Ports.SerialPortStream.NativeSerialPort.CommOverlappedIo.OverlappedIoState..ctor(Int32 readBuffer, Int32 writeBuffer) in C:\xxx\SerialPortStream\code\SerialPortStream.NativeSerialPort.CommOverlappedIo.cs:Zeile 380.
bei RJCP.IO.Ports.SerialPortStream.NativeSerialPort.CommOverlappedIo.Start() in C:\xxx\SerialPortStream\code\SerialPortStream.NativeSerialPort.CommOverlappedIo.cs:Zeile 1012.
bei RJCP.IO.Ports.SerialPortStream.Open(Boolean setCommState) in C:\xxx\SerialPortStream\code\SerialPortStream.cs:Zeile 446.
bei RJCP.IO.Ports.SerialPortStream.Open() in C:\xxx\SerialPortStream\code\SerialPortStream.cs:Zeile 384.
bei COM_Tester.Main.bwSpam_DoWork(Object sender, DoWorkEventArgs e) in C:\xxx\COM Tester\Form1.vb:Zeile 53.

with kind regards
Kevin Gardthausen

file attachments

Closed Aug 1, 2015 at 3:34 PM by jmcurl

comments

jmcurl wrote Jul 21, 2015 at 7:16 PM

Hello Hr. Gardthausen!

Thanks for the report. A memory leak issue was fixed in r32772 on 24.05.2015. With what version did you find the problem? Do you have a test case to reproduce the problem?

I'll look into your patch in detail on the weekend to determine its scope and identify how this might occur.

Mit freundlichen Grüßen,
Jason.

kevingardthausen wrote Jul 22, 2015 at 8:19 AM

Good Day!

This was tested with r32778 (25.05.2015).

Test Code to reproduce (Visual Basic.NET):
        Dim tPort As SerialPortStream
        Dim i As Int32

        Try
            Do While True
                tPort = New SerialPortStream("COM5")

                tPort.Open()
                tPort.Close()
                tPort.Dispose()

                If ((i Mod 10) = 0) Then Call Trace.WriteLine(i.ToString())

                i += 1
            Loop
        Catch iEx As Exception
            Call Trace.WriteLine(iEx.ToString())
            Stop
        End Try
Depending on system this can take between 1.100 to 1.400 iterations before failure.

With kind regards
Kevin Gardthausen

wrote Jul 25, 2015 at 8:45 PM

wrote Aug 1, 2015 at 3:34 PM

wrote Aug 1, 2015 at 3:34 PM