Virtual Memory low. Memory Leak?

BlitzPlus Forums/BlitzPlus Programming/Virtual Memory low. Memory Leak?

weebo(Posted 2004) [#1]
Hi, I have written a pretty basic program designed to sit on a PC and run 24/7. Trouble is after a few hours windws reports virtual memory is running low which I think means there is a flaw in my programming using up more and more memory. Here is the code, perhaps someone can point out the flaw in my program.

; Merlin Whiteboard Display
; Full screen 12 Line display of a Text file created by Whiteboard VB program
; By Neil Corbett

;Global Variables
Dim bayarray$(11,255) ;This array holds all the information (row/column)
Global fntArial ;Font for Display
Global scramblecount ;for matrix effect
Const whiteboard_file$ = "config.dat" ;file containing data
Global quote$

;string manipulation
Dim currentchar(11)
Dim length(11)

;misc
Global updatetimer
Global background_image


; Includes
Include "whiteboard1.bb" ;Static board with scrolling on longer descriptions
Include "whiteboard2.bb" ;Displays the IT Helpline number
Include "whiteboard3.bb" ;Displays the IT Quote of the day



; ==============================================================================================;
; Main Program


initialisevideo

Repeat

;select which view to display on this pass
SeedRnd (MilliSecs())
mode = Rand(1,3)

;load the array for updates
load_array(whiteboard_file)

updatetimer = 0

;Run the preloop code for the selected mode
If mode = 1Then whiteboard1preloop
If mode = 2 Then whiteboard2preloop
If mode = 3 Then whiteboard3preloop

Repeat
;run the random view selected until next pass
If mode = 1 Then whiteboard1loop
If mode = 2 Then whiteboard2loop
If mode = 3 Then whiteboard3loop

updatetimer = updatetimer + 1
If KeyHit(1) Then End
Until updatetimer = 500 Or KeyHit(1)

Until KeyHit(1)

; End of Main Program
; ==============================================================================================;


Function initialisevideo()
;Prepare the variables before running the main loop

;set the resolution
Graphics 1024,768,32,1

;Prepare the Fonts
fntArial=LoadFont("Arial",30,False,False,False)
SetFont fntArial

End Function


Function load_array(file_to_open)

;open the file to read
filein = ReadFile(whiteboard_file)

;Read the Header
quote = ReadLine(filein)

;Read each line in the file to the array
For count = 0 To 11
For count2 = 0 To 3
bayarray(count,count2) = ReadLine(filein)
Next
Next

CloseFile( filein )

;add whitespace to scrolling text
For count = 0 To 11
Length(count) = Len(bayarray(count,1))
If length(count) > 48 Then bayarray(count,1) = " " + bayarray(count,1)
Next

;set the First letter of the descriptions To 1
For count = 0 To 11
currentchar(count) = 1
Next

End Function


****************This is the second file included

Function whiteboard1preloop()

scramblecount = 100

;load the background image
background_image = LoadImage("background.jpg")

;Prepare the Fonts
fntArial=LoadFont("Arial",30,False,False,False)
SetFont fntArial

While scramblecount <> 0 ;Displays scrambling text for a few seconds

SetBuffer BackBuffer()
Cls
DrawImage(background_image,0,0)
updatescramble
Flip

scramblecount = scramblecount - 1
Delay(20)
Wend

End Function

Function whiteboard1loop()

update_changes
update_screen
Delay(200)

End Function

Function updatescramble()

x = 40
For count2 = 0 To 3
If count2 = 1 Then ;this is the description field
Text(x,100,Chr(Rand(97,122)),False,True)
Text(x,150,Chr(Rand(97,122)),False,True)
Text(x,200,Chr(Rand(97,122)),False,True)
Text(x,250,Chr(Rand(97,122)),False,True)
Text(x,300,Chr(Rand(97,122)),False,True)
Text(x,350,Chr(Rand(97,122)),False,True)
Text(x,400,Chr(Rand(97,122)),False,True)
Text(x,450,Chr(Rand(97,122)),False,True)
Text(x,500,Chr(Rand(97,122)),False,True)
Text(x,550,Chr(Rand(97,122)),False,True)
Text(x,600,Chr(Rand(97,122)),False,True)
Text(x,650,Chr(Rand(97,122)),False,True)
End If

If count2 <> 1 Then
Text(x,100,Chr(Rand(80,100)),False,True)
Text(x,150,Chr(Rand(80,100)),False,True)
Text(x,200,Chr(Rand(80,100)),False,True)
Text(x,250,Chr(Rand(80,100)),False,True)
Text(x,300,Chr(Rand(80,100)),False,True)
Text(x,350,Chr(Rand(80,100)),False,True)
Text(x,400,Chr(Rand(80,100)),False,True)
Text(x,450,Chr(Rand(80,100)),False,True)
Text(x,500,Chr(Rand(80,100)),False,True)
Text(x,550,Chr(Rand(80,100)),False,True)
Text(x,600,Chr(Rand(80,100)),False,True)
Text(x,650,Chr(Rand(80,100)),False,True)
End If
If count2 = 0 Then x = x + 55
If count2 = 1 Then x = x + 590
If count2 = 2 Then x = x + 130
Next

End Function

Function update_screen()

SetBuffer BackBuffer()
Cls
DrawImage(background_image,0,0)
draw_text
Flip

End Function

Function update_changes()

For count = 0 To 11
Length(count) = Len(bayarray(count,1))
If currentchar(count) = length(count) Then currentchar(count) = 1
If length(count) > 48 Then currentchar(count) = currentchar(count) + 1
Next

End Function


Function draw_text()

x = 40
For count2 = 0 To 3
If count2 = 1 Then ;this is the description field
Text(x,100,Mid(bayarray(0,count2),currentchar(0),48),False,True)
Text(x,150,Mid(bayarray(1,count2),currentchar(1),48),False,True)
Text(x,200,Mid(bayarray(2,count2),currentchar(2),48),False,True)
Text(x,250,Mid(bayarray(3,count2),currentchar(3),48),False,True)
Text(x,300,Mid(bayarray(4,count2),currentchar(4),48),False,True)
Text(x,350,Mid(bayarray(5,count2),currentchar(5),48),False,True)
Text(x,400,Mid(bayarray(6,count2),currentchar(6),48),False,True)
Text(x,450,Mid(bayarray(7,count2),currentchar(7),48),False,True)
Text(x,500,Mid(bayarray(8,count2),currentchar(8),48),False,True)
Text(x,550,Mid(bayarray(9,count2),currentchar(9),48),False,True)
Text(x,600,Mid(bayarray(10,count2),currentchar(10),48),False,True)
Text(x,650,Mid(bayarray(11,count2),currentchar(11),48),False,True)
End If

If count2 <> 1 Then
Text(x,100,bayarray(0,count2),False,True)
Text(x,150,bayarray(1,count2),False,True)
Text(x,200,bayarray(2,count2),False,True)
Text(x,250,bayarray(3,count2),False,True)
Text(x,300,bayarray(4,count2),False,True)
Text(x,350,bayarray(5,count2),False,True)
Text(x,400,bayarray(6,count2),False,True)
Text(x,450,bayarray(7,count2),False,True)
Text(x,500,bayarray(8,count2),False,True)
Text(x,550,bayarray(9,count2),False,True)
Text(x,600,bayarray(10,count2),False,True)
Text(x,650,bayarray(11,count2),False,True)
End If
If count2 = 0 Then x = x + 55
If count2 = 1 Then x = x + 590
If count2 = 2 Then x = x + 130
Next

End Function

*******************************************************


I am only interested in the whiteboard1 include, the other 2 are blank at present for future additions.

Thanks to anyone who takes a look!


soja(Posted 2004) [#2]
I just glanced at it, but you're putting a whole bunch of things in functions that look like they should onlyu be done once (or if not, you're not freeing the originals before loading another)...

For instance:
a) In your main loop, put Repeat after loadarray or updatetimer=0
b) In your whiteboard1preloop() function, load the image and the font only once. That could mean to put them in another function that is only called ONCE before your Repeat in the main loop.

Stuff like that. There may be more. Basically, just look for where memory is allocated and not freed multiple times (like in your loop). =)


weebo(Posted 2004) [#3]
Hi,

I see what you mean, but that is by design. The program displays information from a file that is updated frequently externally. So the program reloads the file in to the arrays etc on each pass, that's why it is nested within the repeat loop.


soja(Posted 2004) [#4]
I see, then just make sure anytime you call something that allocates memory (like LoadImage), you call the appropriate command to delete/free that memory (like FreeImage) before you load something else with it.

There are some commands which I don't see why you would need to call them more than once, though, e.g. SeedRnd, LoadFont, SetFont.


weebo(Posted 2004) [#5]
Thanks, you have to see the program running really to understand why I'm doing some of the odd things going on, but you have been a great help with the freeimage etc thats sorted it.

My own laziness I guess.