dcsimg
Results 1 to 3 of 3

Thread: SerialPort memory leak when USB device removed

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    14

    SerialPort memory leak when USB device removed

    I'm using .net 4.5 with Windows 7 Pro 64 bit SP1. I have a vb.net program that uses the serial port to communicate with a device. The port is always open. When the device is removed, memory goes up significantly. It looks like Microsoft used to have a bug that was supposedly resolved in 4.0. I added code for the workaround to that bug.

    Now, sometimes when I compile my program the memory goes up indefinitely when the device is removed, and sometimes the memory goes up alot, then levels off at a high value. Reconnecting the device and port, then disconnecting again ups the memory to a new higher level.

    Any idea on other ways I can clear out the memory when the device is removed?

    I've made a small program that duplicates the error, but most of the time it does not rise indefinitely on the device being disconnected. It just rises to a high level.

    I'm monitoring memory using perfmon and looking at Private Bytes.

    My small program has the same imports as the main program, otherwise I made it as small as possible. Contains:

    New - opens up the port
    Override WndProc - the bug workaround
    reconnect - to reconnect the port


    Code:
    Imports System.IO.Ports
    Imports System.Threading
    Imports System.Text.RegularExpressions
    Imports System.IO
    Imports System.Runtime.InteropServices
    Imports System.Security.AccessControl
    
    Public Class Form1
        Dim WithEvents COMPort As New SerialPort
        Dim portname As String = "COM2"
    
        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
    
            COMPort.PortName = portname
            COMPort.BaudRate = 9600
            COMPort.ReadTimeout = 2000
            COMPort.DataBits = 8
            COMPort.Parity = Parity.None
            COMPort.StopBits = 1
    
            Dim portexists As Integer = 0
            For Each s As String In SerialPort.GetPortNames()
                If (portname = s) Then
                    portexists = 1
                End If
            Next
    
            If (portexists = 0) Then
                MsgBox("Device not connected to port " & portname)
            Else
                Try
                    COMPort.Open()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
    
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If (m.Msg = 537) Then
                Dim WParam As Integer = CInt(m.WParam)
                If ((WParam And &H8000) = &H8000) Then
                    'If (COMPort.IsOpen) Then
                    Try
                        'COMPort.DiscardInBuffer()
                        'COMPort.DiscardOutBuffer()
                        COMPort.Close()
                        COMPort.Dispose()
                        'COMPort = New SerialPort
                    Catch ex As Exception
                        'MsgBox(ex.ToString)
                    End Try
                    'End If
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            reconnect()
        End Sub
    
        Public Sub reconnect()
    
            If (COMPort.IsOpen = True) Then
                COMPort.Close()
            End If
    
            COMPort.PortName = portname
            COMPort.BaudRate = 9600
            COMPort.ReadTimeout = 2000
            COMPort.DataBits = 8
            COMPort.Parity = Parity.None
            COMPort.StopBits = 1
    
    
            Dim portexists As Integer = 0
            For Each s As String In SerialPort.GetPortNames()
                If (portname = s) Then
                    portexists = 1
                End If
            Next
    
            If (portexists = 0) Then
                MsgBox("Device not connected to port " & portname)
            Else
                Try
                    COMPort.Open()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
    
        End Sub
    End Class

    Thanks!

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,744

    Re: SerialPort memory leak when USB device removed

    I don't see that you call COMPort.Dispose anywhere.
    I wonder if that would help.
    Normally several background threads are created in conjunction with the SerialPort object, and each thread probably has allocated some buffer space. The threads might still be cycling and because of the port no longer existing, generating some type of leak. I don't know if there is a way to identify the threads and get rid of them. If you're lucky, calling Dispose on the object after closing it would clean up extra stuff that may keep the threads active.
    But I don't really know.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    14

    Re: SerialPort memory leak when USB device removed

    Thanks for the response.

    WndProc has COMPort.Dispose mentioned. It doesn't seem to do anything though. I added it to the reconnect subroutine as well as per your suggestion. It doesn't lower the memory usage when the reconnect button is pressed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width