Memory Leak

MTSkull

Well-known member
Joined
Mar 25, 2003
Messages
135
Location
Boulder, Colorado
The following code causes an Out Of Memory Error when it cycles continuously as a screen saver, any thoughts.

Code:
  Public bFirstMove As Boolean = True
    Public Pics() As String
    Public pos As Int16

    Private Sub Form1_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) _
           Handles MyBase.Load

        Me.CenterToScreen()

        picBox.Height = Me.Height
        picBox.Width = Me.Width

        Pics = Directory.GetFiles("C:\Pictures")
        pos = 0
        picBox.Image = Drawing.Bitmap.FromFile(Pics(pos))

        Me.Cursor.Hide()

    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.KeyEventArgs) _
          Handles MyBase.KeyUp

        Me.Close()

    End Sub

    Private Sub picBox_MouseMove(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.MouseEventArgs) _ 
           Handles picBox.MouseMove

        Static iCount As Long
        If iCount > 2 Then
            Me.Close()
        Else
            iCount = iCount + 1
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles Timer1.Tick

        pos += 1
        If pos = UBound(Pics) Then
            pos = 0
        End If

        picBox.Image = Drawing.Bitmap.FromFile(Pics(pos))

    End Sub
 
How quickly is the timers interval set for? And after how long does
the error occur?
 
Bucky,
The timer tick event is set to 5000 (5 seconds), error occures after an hour or so.

samsmithnz,
Well the short awnser (and not meaning to be a smart ***) is that was how I wrote it, however, I am open to suggestions if you know a better way.

As it is stands, it runs for about an hour before it crashes. I set the form to normal (it is usually set to Maximize) and then ran it and opened the task manager to watch what happened to the memory. I had thought it was overloading when it rolled over, but the memory steadly increased as each new picture was displayed. So I am currently running a test where I placed this code in the timer tick event
Code:
picBox.Image.Dispose
picBox.Image = Drawing.Bitmap.FromFile(Pics(pos))

On my quick test this seemed to work but I am running an extended test now (1:30). If that works I will leave it on overnight.

Thanks
Brian
 
I have had similar problems with some image handling things until I found a solution which were .Dispose ..... my program crashed faster tough ... anyway try to look for some flying objects which are eating up memory...
 
Theres a known issue with a Picture box loading pictures directly from disk - basically it holds onto that image. You can test this by loading a picture from disk into a picturebox using the load method then trying to overwrite that image on disk (you cant).

I think the workaround was to load the picture into an Image, then load the Image object into a Bitmap and assign that bitmap to the pictureboxs image property. Then you can dispose of the Image and the PictureBox will cleanup after itself.

-Nerseus
 
But its not the PictureBox doing the load - it is a shared method of the Bitmap class. Maybe the problem is with using the Bitmap class to do the load and the workaround is to use the Image class?
 
Back
Top