Question Memory Problems

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Hi guys i have a problem with a block of code in a DLL i have written, the DLL receives Serial Data (NMEA) and then logs it to a file, it also holds in a dictionary (of string and date) the sentence header and the date and time it was last received).

I monitor the Task manager Thread-count and memory usage and it is going up quickly and after about 2.5 hours of running my program crashes with an out of memory error.

the code is below, if anyone can see where i am not releasing something or i am doing something fundamentally wrong please let me know as i am scratching my head at the moment, i added the code for serial data recieved and also the the managing of the dictionary (if a sentence isn't recieved for 5 minutes then it is removed from the list), finally there is a function to recover the date and time from the GPS NMEA Sentance:

Code:
 Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim ff As Boolean
            If SerialPort1.IsOpen = True Then
                Do
                    Dim incoming As String = SerialPort1.ReadLine()
                    If incoming Is Nothing Then
                        Exit Do
                    Else
                        If first = False Then
                            ff = Write_Data(incoming)
                            If ff = True Then
                                lastData = DateTime.Now
                            End If
                            Dim cursen As String
                            cursen = incoming.Substring(0, incoming.IndexOf(","))
                            addNMEA(cursen)
                            If cursen = "$GPZDA" Then
                                CurrentGPSDateTime = recoverDateTime(incoming)
                            End If
                        Else
                            first = False
                        End If
                    End If
                Loop
            End If
            manageNMEAlist()
        Catch
        End Try
    End Sub
    
       Private Function recoverDateTime(ByVal NMEA As String) As Date
        Try
            Dim fields(), timeString As String
            Dim timeArray() As Char
            fields = NMEA.Split(",")
            timeArray = fields(1).ToCharArray
            timeString = fields(3) & "/" & fields(2) & "/" & fields(4) & " " & timeArray(0) & timeArray(1) & ":" & timeArray(2) & timeArray(3) & ":" & timeArray(4) & timeArray(5)
            Return DateTime.ParseExact(timeString, "MM/dd/yyyy HH:mm:ss", Nothing)
        Catch
            Return #1/1/2001#
        End Try
    End Function
    
        Private Function manageNMEAlist()
        Try
            Dim pair As KeyValuePair(Of String, Date)
            For Each pair In currentNMEAList
                If pair.Value < DateTime.Now.AddMinutes(-5) Then
                    currentNMEAList.Remove(pair.Key)
                End If
            Next

            Return True
        Catch
            Return False
        End Try

    End Function

any thoughts would be appreciated.

Thanks

Stu
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
I have changed the code to :

Code:
   [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] SerialPort1_DataReceived(sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], e [COLOR=blue]As[/COLOR] IO.Ports.[COLOR=#2b91af]SerialDataReceivedEventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] SerialPort1.DataReceived
        [COLOR=blue]Try[/COLOR]
            [COLOR=blue]Dim[/COLOR] ff [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR]
            [COLOR=blue]If[/COLOR] SerialPort1.IsOpen = [COLOR=blue]True[/COLOR] [COLOR=blue]Then[/COLOR]
                [COLOR=blue]If[/COLOR] first = [COLOR=blue]False[/COLOR] [COLOR=blue]Then[/COLOR]
                    ReceiveBuffer += SerialPort1.ReadExisting
                    [COLOR=blue]If[/COLOR] InStr(ReceiveBuffer, vbCrLf) [COLOR=blue]Then[/COLOR]
                        [COLOR=blue]Dim[/COLOR] rxlines() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = Split(ReceiveBuffer, vbCrLf)
                        [COLOR=blue]For[/COLOR] i = 0 [COLOR=blue]To[/COLOR] UBound(rxlines) - 1
                            [COLOR=blue]If[/COLOR] rxlines(i).Length > 5 [COLOR=blue]Then[/COLOR]
                                ff = Write_Data(rxlines(i))
                                [COLOR=blue]If[/COLOR] ff = [COLOR=blue]True[/COLOR] [COLOR=blue]Then[/COLOR]
                                    lastData = [COLOR=#2b91af]DateTime[/COLOR].Now
                                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                                [COLOR=blue]Dim[/COLOR] cursen [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
                                cursen = rxlines(i).Substring(0, rxlines(i).IndexOf([COLOR=#a31515]","[/COLOR]))
                                addNMEA(cursen)
                                [COLOR=blue]If[/COLOR] cursen = [COLOR=#a31515]"$GPZDA"[/COLOR] [COLOR=blue]Then[/COLOR]
                                    CurrentGPSDateTime = recoverDateTime(rxlines(i))
                                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                        [COLOR=blue]Next[/COLOR]
                        ReceiveBuffer = rxlines(UBound(rxlines))
                    [COLOR=blue]Else[/COLOR]
                        [COLOR=blue]If[/COLOR] ReceiveBuffer.Length > 4000 [COLOR=blue]Then[/COLOR]
                            ReceiveBuffer = [COLOR=#a31515]""[/COLOR]
                        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]Else[/COLOR]
                    first = [COLOR=blue]False[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
            manageNMEAlist()
        [COLOR=blue]Catch[/COLOR]
        [COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
and so far the thread number and memory seem stable, i will post any outcomes, but if anyone sees a problem with my original code please let me know so i can understand what i was doing wrong.

Thanks
 
Top Bottom