memleak - I'm blind!

BlitzMax Forums/BlitzMax Programming/memleak - I'm blind!

Hi all,

Can you see any obvious leaks in this code? I swear it's obvious but I just can't find it!!!

If you run it and watch taskman, you'll see it rise and rise and rise. This however doesn't get anyone excited.

Thanks for you help!

Framework BRL.D3D7Max2D
Import brl.Random

Graphics 640,480

Global pixmap1:TPixmap=GrabPixmap(0,0,640,480);ClearPixels(pixmap1,$00ffffff)
Global pixmap2:TPixmap=GrabPixmap(0,0,640,480);ClearPixels(pixmap2,$002000ff)
For n=1 To 10
	Swapscreens(pixmap1,pixmap2,10,10,Rand(1,16),10)
Next

End

Function Swapscreens(Fromimage:TPixmap,toimage:TPixmap,Xcount,Ycount,style=0,lifemult#=3)
'GCSuspend()	'disable GC whilst fading. Seems to bring up the speed a little.
				'Removing this line doesn't help with the memleak ;)

	Local oldrot#=GetRotation()
	Type tchunk
		Field image:TImage
		Field pixmap:TPixmap
		Field life#
		Field rot:Float
		Field x#,y#
		Field movex#,movey#=-1
		Field xrotspeed#=Rnd(0,10),yrotspeed#=Rnd(0,10)
		Field xrot#,yrot#
		Field rotspeed#=Rnd(-12,12)
		Method Delete()
		Self.pixmap=Null
		Self.image=Null
		End Method
	End Type
	Local width:Int=fromimage.width
	Local height:Int=fromimage.height

	Local chunklist:TList=New TList
	Local chunksx=Xcount
	Local chunksy=Ycount
	Local chunksizex#=Float(width/chunksx)
	Local chunksizey#=Float(height/chunksy)
	Local toimage2=LoadImage(toimage)
	Local fromimage2=LoadImage(fromimage)
	
	DrawImage fromimage2,0,0
	Flip
	
	For x=0 To chunksx-1
		For y=0 To chunksy
			Local startx=(x*chunksizex)
			Local starty=(y*chunksizey)
			Local chunk:tchunk=New tchunk
			chunk.x=startx+(chunksizex/2)
			chunk.y=starty+(chunksizey/2)
			chunk.life=1
			If style=0 Then chunk.life=Abs(x-(chunksx/2.0))+Abs(y-(chunksy/2.0))
			If style=1 Then chunk.life=(Abs((chunksx/2.0))+Abs((chunksy/2.0)))-(Abs(x-(chunksx/2.0))+Abs(y-(chunksy/2.0)))
			If style=2 Then chunk.life=(x+y)
			If style=3 Then chunk.life=(chunksx-x)+(chunksy-y)
			If style=4 Then chunk.life=y
			If style=5 Then chunk.life=(chunksy-y)
			If style=6 Then chunk.life=Abs(x-(chunksx/2.0))
			If style=7 Then chunk.life=Abs(y-(chunksy/2.0))
			If style=8 Then chunk.life=chunksx-Abs(x-(chunksx/2.0))
			If style=9 Then chunk.life=chunksy-Abs(y-(chunksy/2.0))
			If style=10 Then chunk.life=(chunksx+chunksy)-Abs((y+x)-(chunksy+chunksx)/2.0)
			If style=11 Then chunk.life=Abs((y+x)-(chunksy+chunksx)/2.0)
			If style=12 Then chunk.life=Abs((Sin(x*10)+Cos(y*20)))*10
			If style=13 Then chunk.life=Abs(((1-Sin(x*10))+(1-Cos(y*20))))*10
			If style=14 Then chunk.life=Abs((Sin(x*10)-Cos(y*20)))*10
			If style=15 Then chunk.life=Abs((-Sin(x*40)+Cos((y*40)+180)))*10
			If style=16 Then chunk.life=20-Abs((Sin(x*40)+Cos((y*40))))*10
			chunk.life=(chunk.life/Float((chunksX+chunksY)/2.0))*10.0
			chunk.life=Int((chunk.life*lifemult)+1)
		
			'Create image for this chunk!
			chunk.image=CreateImage(chunksizex,chunksizey)
			'Grab from backbuffer into created image.
			GrabImage(chunk.image,startx,starty)
			
			
			SetImageHandle(chunk.image,chunksizex/2,chunksizey/2)
			ListAddLast chunklist,chunk
		Next
	Next

	Repeat
		count=0
		Cls
		SetRotation 0
		DrawImage toimage2,0,0
		Delay 1
		For chunk:tchunk=EachIn chunklist
			SetRotation chunk.rot
			If chunk.life<0 Then
				chunk.rot=chunk.rot+chunk.rotspeed
			EndIf
			DrawImage chunk.image,chunk.x,chunk.y
			chunk.life=chunk.life-1;If chunk.life=0 Then chunk.movey=Rnd(-3,-5);chunk.movex=Rnd(-5,5)
			If chunk.life<0 Then
				chunk.x=chunk.x+chunk.movex
				chunk.y=chunk.y+chunk.movey
				chunk.movey=chunk.movey+.3
			EndIf
			If chunk.x>GraphicsWidth()+chunksizex Or chunk.x<-chunksizex Or chunk.y>GraphicsHeight()+chunksizey Then ListRemove chunklist,chunk;chunk.image=Null;
			count=count+1
		Next
		Flip True
		If KeyHit(key_escape) Then End
	Until count=0
	SetRotation oldrot
	DebugLog chunklist.count()
'	GCResume()

End Function


	Local toimage2:TImage=LoadImage(toimage)
	Local fromimage2:TImage=LoadImage(fromimage)

GCMemalloced() wasn't showing any leak so it must have been something unGCed. As your code didn't specify an object type it was int (not covered by GC) so was taking the space each time and never being released.

Oh, cheers. i'll take a look :)

[edit]

Thanks, thats great! :)


[edit]

In future, use superstrict. :)