VB .NET memory leak

Discussion in 'OT Technology' started by GOGZILLA, May 13, 2006.

  1. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    I've got a memory leak in this program somewhere. Its a pocket pc app and when I exit it depending on how long its been running memory is just gone and does not come back until a restart of the device. If anyone here is familiar with VB .NET take a look at this and see


    Code:
        Private Sub PlotValue(ByVal old_y As Integer, ByVal new_y As Integer)
            ' Make the Bitmap and Graphics objects.
            Dim wid As Integer = picGraph.ClientSize.Width
            Dim hgt As Integer = picGraph.ClientSize.Height
            Dim bm As New Bitmap(wid, hgt)
            Dim gr As Graphics = Graphics.FromImage(bm)
    
            ' Move the old data one pixel to the left.
            gr.DrawImage(picGraph.Image, -1, 0)
    
            'Scaling
            old_y = hgt - (hgt * (old_y / 1023))
            new_y = hgt - (hgt * (new_y / 1023))
    
            Label6.Text = old_y
            Label7.Text = new_y
    
            ' Erase the right edge and draw guide lines.
            gr.DrawLine(New Pen(Color.White), wid - 1, 0, wid - 1, hgt - 1)
            For i As Integer = 0 To hgt Step yStep
                gr.DrawLine(New Pen(Color.Black), wid - 2, i, wid - 1, i)
            Next i
            ' Plot a new pixel.
            gr.DrawLine(New Pen(Color.Red), wid - 2, old_y, wid - 1, new_y)
    
            ' Display the result.
            picGraph.Image = bm
            picGraph.Refresh()
    
            gr.Dispose()
    
    
        End Sub
    
    This gets called everytime I receive a new data value which is pretty continuous. I'm not sure why its killing the memory so bad my guess would be the New Bitmap everytime and its not properly disposing of those.
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    After that routine runs about 250 times I get the out of memory error. If I look at the memory manager the pocket pc starts with about 29mb's of memory and after around those 250 times it down to about 8mb's of memory and the bad thing is that after exiting the program the memory isn't released.
     
  3. w0lverine

    w0lverine New Member

    Joined:
    Feb 22, 2005
    Messages:
    47
    Likes Received:
    0
    GC.Collect() :dunno: ... And as you said, you should dispose the old Bitmap before creating a new one ... maybe this works, but I'm not exactly sure what is wrong with that code
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Nice dude! that fixed it I tried reading up on it some but theres alot of memory problems with .NET dealing with automatic cleanup of bitmap objects and stuff. I was trying to do bm.Dispose() but it was giving me drawing errors with it so i just do gr.Dispose() and then GC.Collect() I probably wouldve never figured that out by myself thanks man :bigthumb:
     
  5. w0lverine

    w0lverine New Member

    Joined:
    Feb 22, 2005
    Messages:
    47
    Likes Received:
    0
    Cool :x: but somehow it's weird, because the garbage collector *should* do this automatically by now ... .NET seems to have these problems with GDI+ since version 1.0 (i remember having the same problem ~4 years ago :rolleyes: )
     

Share This Page