A memory leak with my types

BlitzPlus Forums/BlitzPlus Programming/A memory leak with my types

Function battle(group$)

hitsound = LoadSound("sound/hit.mp3")

                charfirst = 0
	charsecond = 0
	charthird = 0
	charfourth = 0
	For i = 1 To 19
		If(charposition(i) = 1)
			charfirst = i
		EndIf
	Next
	For i = 1 To 19
		If(charposition(i) = 2)	
			charsecond = i
		EndIf
	Next
	For i = 1 To 19
		If(charposition(i) = 3)
			charthird = i
		EndIf
	Next
	For i = 1 To 19
		If(charposition(i) = 4)
			charfourth = i
		EndIf
	Next




	If(Not(charfirst = 0))
		battlechar1.battlechar = New battlechar
		battlechar1\name$ = charname$(charfirst)
		battlechar1\image$ = charimagefile$(charfirst)
		battlechar1\health = charhealth(charfirst)
		battlechar1\mana = charmana(charfirst)
		battlechar1\attack = charattack(charfirst)
		battlechar1\defence = chardefence(charfirst)
		battlechar1\speed = charspeed(charfirst)
		battlechar1\alive = charalive(charfirst)
		battlechar1\x = 16
		battlechar1\y = 16
		battlechar1\mode = RUNNING

	EndIf
	If(Not(charsecond = 0))
		battlechar2.battlechar = New battlechar
		battlechar2\name$ = charname$(charsecond)
		battlechar2\image$ = charimagefile(charsecond)
		battlechar2\health = charhealth(charsecond)
		battlechar2\mana = charmana(charsecond)
		battlechar2\attack = charattack(charsecond)
		battlechar2\defence = chardefence(charsecond)
		battlechar2\speed = charspeed(charsecond)
		battlechar2\alive = charalive(charsecond)
		battlechar2\x = 16
		battlechar2\y = 16
		battlechar2\mode = AI


	EndIf
	If(Not(charthird = 0))
		battlechar3.battlechar = New battlechar
		battlechar3\name$ = charname$(charthird)
		battlechar3\image$ = charimagefile(charthird)
		battlechar3\health = charhealth(charthird)
		battlechar3\mana = charmana(charthird)
		battlechar3\attack = charattack(charthird)
		battlechar3\defence = chardefence(charthird)
		battlechar3\speed = charspeed(charthird)
		battlechar3\alive = charalive(charthird)
		battlechar3\x = 16
		battlechar3\y = 16
		battlechar3\mode = AI

	EndIf
	If(Not(charfourth = 0))
		battlechar4.battlechar = New battlechar
		battlechar4\name$ = charname$(charfourth)
		battlechar4\image$ = charimagefile(charfourth)
		battlechar4\health = charhealth(charfourth)
		battlechar4\mana = charmana(charfourth)
		battlechar4\attack = charattack(charfourth)
		battlechar4\defence = chardefence(charfourth)
		battlechar4\speed = charspeed(charfourth)
		battlechar4\alive = charalive(charfourth)
		battlechar4\x = 16
		battlechar4\y = 16
		battlechar4\mode = AI

	EndIf






	character = 1






num = Rnd(1,9)

filein = ReadFile("mygame/monster/monstergroup/" + group$ + num + ".dat")

monnumber = ReadInt(filein)

For i = 1 To monnumber
	monsterpath$(i) = ReadString(filein)
	monx(i) = ReadInt(filein)
	mony(i) = ReadInt(filein)
Next

CloseFile(filein)

For i = 1 To monnumber
	filein = ReadFile(monsterpath$(i))
	monster.monster = New monster
	
	monster\name$ = ReadString(filein)
	monster\image$ = ReadString(filein)
	monster\skill$ = ReadString(filein)
	
	monster\health = ReadInt(filein)
	monster\mana = ReadInt(filein)
	monster\attack = ReadInt(filein)
	monster\defence = ReadInt(filein)
	monster\speed = ReadInt(filein)
	monster\gold = ReadInt(filein)
	monster\experience = ReadInt(filein)
	
	monster\x = monx(i)
	monster\y = mony(i)
	CloseFile(filein)
	
Next




testup = LoadImage("graphics/swordtestup.bmp")
testdown = LoadImage("graphics/swordtestdown.bmp")
	














While (never = 0)
	Cls
	

	For monster.monster = Each monster
		monsterdrawing = LoadImage(monster\image$)
		DrawImage monsterdrawing,monster\x,monster\y
		monster\x = monster\x + Rnd(-2,2)
		monster\y = monster\y + Rnd(-2,2)
	Next
	

;CHARACTER SELECTION
	If KeyHit(ONEKEY)
		If(Not(charfirst = 0))
			battlechar1\mode = RUNNING
		EndIf
		If(Not(charsecond = 0))
			battlechar2\mode = AI
		EndIf
		If(Not(charthird = 0))
			battlechar3\mode = AI
		EndIf
		If(Not(charfourth = 0))
			battlechar4\mode = AI
		EndIf
	EndIf
	If KeyHit(TWOKEY)
		If(Not(charfirst = 0))
			battlechar1\mode = AI
		EndIf
		If(Not(charsecond = 0))
			battlechar2\mode = RUNNING
		EndIf
		If(Not(charthird = 0))
			battlechar3\mode = AI
		EndIf
		If(Not(charfourth = 0))
			battlechar4\mode = AI
		EndIf
	EndIf
	If KeyHit(THREEKEY)
		If(Not(charfirst = 0))
			battlechar1\mode = AI
		EndIf
		If(Not(charsecond = 0))
			battlechar2\mode = AI
		EndIf
		If(Not(charthird = 0))
			battlechar3\mode = RUNNING
		EndIf
		If(Not(charfourth = 0))
			battlechar4\mode = AI
		EndIf
	EndIf
	If KeyHit(FOURKEY)
		If(Not(charfirst = 0))
			battlechar1\mode = AI
		EndIf
		If(Not(charsecond = 0))
			battlechar2\mode = AI
		EndIf
		If(Not(charthird = 0))
			battlechar3\mode = AI
		EndIf
		If(Not(charfourth = 0))
			battlechar4\mode = RUNNING
		EndIf
	EndIf
;CHARACTER SELECTION
	
	
	

;Character Loop	
For battlechar.battlechar = Each battlechar


	If(battlechar\mode = RUNNING)
		If KeyDown(UPKEY)
			battlechar\y = battlechar\y - 3

		EndIf
		If KeyDown(DOWNKEY)
			battlechar\y = battlechar\y + 3

		EndIf
		If KeyDown(RIGHTKEY)
			battlechar\x = battlechar\x + 3
			battlechar\face = FACERIGHT
		EndIf
		If KeyDown(LEFTKEY)
			battlechar\x = battlechar\x - 3
			battlechar\face = FACELEFT
		EndIf
		If KeyHit(SPACEKEY)
			If(battlechar\face = FACERIGHT)
				DrawImage testdown,battlechar\x + 35,battlechar\y
				For monster.monster = Each monster
					tempmon = LoadImage(monster\image)
					If(ImagesOverlap(testdown,battlechar\x + 35, battlechar\y,tempmon,monster\x,monster\y))
						monster\health = monster\health - 10
						PlaySound(hitsound)
					EndIf
				Next
			EndIf
			If(battlechar\face = FACELEFT)
				DrawImage testdown,battlechar\x - 35,battlechar\y
				For monster.monster = Each monster
					If((monster\x <= battlechar\x) And (monster\x >= (battlechar\x - 40)))
						If((monster\y >= (battlechar\y - 15)) And (monster\y <= (battlechar\y + 15)))
							monster\health = monster\health - 10
							PlaySound(hitsound)
						EndIf
					EndIf
				Next
			EndIf
			battlechar\mode = RECOVER
			battlechar\attackagain = 0
		EndIf
	EndIf
	
	If(battlechar\mode = RECOVER)
		battlechar\attackagain = battlechar\attackagain + battlechar\speed
		If(battlechar\attackagain >= 200)
			battlechar\mode = RUNNING
			FlushKeys()
		EndIf
	EndIf

	If(battlechar\mode = AI)
		For monster.monster = Each monster
			mydistance = 10000
			myx = 0
			myy = 0
			tempx = monster\x - battlechar\x
			tempy = monster\y - battlechar\y

			distance = tempx + tempy
			If(distance <= mydistance)
				mydistance = distance
				myx = tempx
				myy = tempy
			EndIf
		Next
		
		
		If(((myy > -20) And (myy <= -5)) Or ((myy < 20) And (myy >=5)))
			If(((myx > -20) And (myx <= 0)) Or ((myx < 20) And (myx >=0)))
				If(battlechar\face = FACEUP)
				DrawImage testup,battlechar\x,battlechar\y - 35
					For monster.monster = Each monster
						If((monster\y <= battlechar\y) And (monster\y >= (battlechar\y - 40)))
							If((monster\x >= (battlechar\x - 15)) And (monster\x <= (battlechar\x + 15)))
								monster\health = monster\health - 10
								PlaySound(hitsound)
							EndIf
						EndIf
					Next
				EndIf
				If(battlechar\face = FACEDOWN)
					DrawImage testup,battlechar\x,battlechar\y + 35
					For monster.monster = Each monster
						If((monster\y >= battlechar\y) And (monster\y <= (battlechar\y + 40)))
							If((monster\x >= (battlechar\x - 15)) And (monster\x <= (battlechar\x + 15)))
								monster\health = monster\health - 10
								PlaySound(hitsound)
							EndIf
						EndIf
					Next
				EndIf
				If(battlecharface = FACERIGHT)
					DrawImage testdown,battlechar\x + 35,battlechar\y
					For monster.monster = Each monster
						If((monster\x >= battlechar\x) And (monster\x <= (battlechar\x + 40)))
							If((monster\y >= (battlechar\y - 15)) And (monster\y <= (battlechar\y + 15)))
								monster\health = monster\health - 10
								PlaySound(hitsound)
							EndIf
						EndIf
					Next
				EndIf
				If(battlecharface = FACELEFT)
					DrawImage testdown,battlechar\x - 35,battlechar\y
					For monster.monster = Each monster
						If((monster\x <= battlechar\x) And (monster\x >= (battlechar\x - 40)))
							If((monster\y >= (battlechar\y - 15)) And (monster\y <= (battlechar\y + 15)))
								monster\health = monster\health - 10
								PlaySound(hitsound)
							EndIf
						EndIf
					Next
				EndIf
	
				battlechar\attackagain = 0
				battlechar\mode = AIRECOVER

			EndIf
		EndIf





		
		
		If(myy < -10)
			battlechar\y = battlechar\y - 3
			battlechar\face = FACEUP
		EndIf
		If(myy > 10)
			battlechar\y = battlechar\y + 3
			battlechar\face = FACEDOWN
		EndIf
		If(myx > 10)
			battlechar\x = battlechar\x + 3
			battlechar\face = FACERIGHT
		EndIf
		If(myx < -10)
			battlechar\x = battlechar\x - 3
			battlechar\face = FACELEFT
		EndIf
		
		
	EndIf


	
	;ATTACKING LOOP
	;ATTACKING LOOP
	
	
If(battlechar\mode = AIRECOVER)
	battlechar\attackagain = battlechar\attackagain + battlechar\speed
	If(battlechar\attackagain >= 200)
		battlechar\mode = AI
	EndIf
EndIf
	
Next
	
	
	;Monster Alive?
	For monster.monster = Each monster
		If(monster\health <= 0)
			Delete monster
		EndIf
	Next
	
	
	For battlechar.battlechar = Each battlechar
		charimage = LoadImage(battlechar\image$)
		DrawImage charimage,battlechar\x,battlechar\y
	Next
	
	If KeyHit(1)
		For battlechar.battlechar = Each battlechar
			Delete battlechar
		Next
		For monster.monster = Each monster
			Delete monster
		Next
		Exit
	EndIf
	
	i = 0
	
	
	For monster.monster = Each monster
		i = i + 1
	Next
	
	If(i = 0)
		For battlechar.battlechar = Each battlechar
			Delete battlechar
		Next
		Exit
	EndIf
	
	
	
	Flip
Wend


End Function





I know I've fubar'd something up.
Whenever I enter battle the memory required to run my program increases at a large, steady rate. I think this has something to do with my types, and was wondering if anyone can see any obvious errors.

Nevermind! It was cuz' I didn't freeimage
Works like a charm now