I have a VB.NET 2003 application that cycles through images and videos on a timer event. After a few hours of operation it 'bombs' with a 'out of memory ' error... ANY HELP would be appreciated. I attach the relevant code sections
---- Source Code ----
Private Sub InitVideoPlayback()
Dim appPath As String
appPath = Application.StartupPath
appPath += "\video"
Try
Dim dir As New System.IO.DirectoryInfo(appPath)
ImgVideoFiles = dir.GetFiles()
NextVideo()
Catch e As Exception
MsgBox("Could not load video " + appPath + ". " + e.Message, MsgBoxStyle.OKOnly)
End Try
End Sub
Private Sub InitProducts()
Dim apppath As String
apppath = Application.StartupPath
apppath += "\products"
Try
Dim img As System.Drawing.Image
Dim dir As New System.IO.DirectoryInfo(apppath)
Dim file As System.IO.FileInfo
ImgFiles = dir.GetFiles()
NextPicture()
Timer = New Timer()
Timer.Interval = 8000
Timer.Start()
Catch e As Exception
MsgBox("Could not load products from " + apppath)
End Try
End Sub
Private Sub NextVideo()
Static Dim Init As Boolean = False
If ImgVideoFiles.Length = 1 And Init Then
myVideo.CurrentPosition = 0
myVideo.Play()
Exit Sub
End If
If Not Init Then Init = True
Try
myVideo.Stop()
Catch e As Exception
'ignore
End Try
Static Dim PicVNum As Integer = 0
Try
If ImgVideoFiles.Length = 0 Then
Exit Sub
End If
If PicVNum >= ImgVideoFiles.Length Then
PicVNum = 0
End If
Try
Dim TmpVid As Microsoft.DirectX.AudioVideoPlayback.Video
myVideo.Stop()
TmpVid = myVideo
TmpVid = Nothing
Application.DoEvents()
Catch ex As Exception
'ignore
End Try
myVideo = New Microsoft.DirectX.AudioVideoPlayback.Video(ImgVideoFiles(PicVNum).FullName())
myVideo.Owner = Me.pnlVideo
myVideo.HideCursor()
myVideo.Play()
Catch e As Exception
MsgBox(e.Message)
End Try
PicVNum += 1
Application.DoEvents()
End Sub
Private Sub NextPicture()
Static Dim PicNum As Integer = 0
If ImgFiles.GetLength(0) = 0 Then
Exit Sub
End If
If PicNum = ImgFiles.GetLength(0) Then
PicNum = 0
End If
Try
pbLeftPic.Image = System.Drawing.Image.FromFile(ImgFiles(PicNum).FullName)
Catch e As Exception
'ignore load errors
End Try
PicNum += 1
pbLeftPic.Refresh()
End Sub

Out of Memory Error - Framework 1.1
Sherchan
ImgFiles and ImgVideoFiles are global (to the form) variables that are initialised at form new and from then onward only used for reading the next file name into the picture or video load code. It is disposed when the program ends.
myVideo is a global (declared in a base form which is inherited). The reason for this is that several events need to pause and play the video so that other sound (specifically Microsoft Agent ) can be played. I have tried to dispose myVideo before doing the creation of the New video assignment to myVideo, but this ends up throwing a General Application Error.
I agree that is definately memory leakage, both on the picturebox and the video file side, but am looking for ways to prevent this / manage this.
Thanks for the reply
Fawadb
Jamie Thomson
Image.FromFile will throw an OutOfMemoryException when the file is not a recognized format or GDI+ does not support the image format.
http://msdn2.microsoft.com/en-us/library/stf701f5.aspx
RosarioCappello
I have solved the problem with my memory errors. On the picturebox side, the image had to be disposed as per the image.dispose method as suggested. The directx proved more difficult. Like most things, once it is solved, it seems simpler than when you were having the problem. My software used the directx.endvideo event to load the next video by calling NextVideo().
In retrospect, this is where the problem lies. I was disposing the video and creating a new video directx connection from the NextVideo procedure. Unforturnately I did this while handling the existing video endvideo event! So I was trying to dispose the current video in the endvideo event and when the event finished, it had nowhere to return to, hence a very cryptic error!
(-: