Memory leak or defective programmer ?
BlitzMax Forums/MiniB3D Module/Memory leak or defective programmer ?
| ||
| I'm pretty new to miniB3D and I'm a little confused, I'm making a game using miniB3D (so easy to use) and JV-ODE which I bought. As I'm coding I noticing that FreeEntity doesn't seem to free the memory being used by the objects. I just modded for demonstation purposes the primitives example included with miniB3D. If I add tons of balls and then remove them the ram doesn't free up. I'm probably doing something wrong but I swear I don't know what. Help Plz !
Import "minib3d.bmx"
Local width=640,height=480,depth=16,mode=0
Graphics3D width,height,depth,mode
Local cam=CreateCamera()
PositionEntity cam,0,0,-10
Local light=CreateLight(1)
Local MrBall:TestBall
Global tex=LoadTexture("examples\media\test.png")
Global BallList:TList=CreateList()
;' used by camera code
Local mxs#=0
Local mys#=0
Local move#=0.5
MouseXSpeed() ;' flush
MouseYSpeed() ;' flush
;' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps
While Not KeyDown(KEY_ESCAPE)
If KeyHit(KEY_ENTER) Then DebugStop
;'' control camera
;' mouse look
If KeyDown(KEY_SPACE)=False
mxs#=mxs#+(MouseXSpeed()/5.0)
mys#=mys#+(MouseYSpeed()/5.0)
RotateEntity cam,mys#,-mxs#,0
MoveMouse width/2,height/2
EndIf
If KeyDown(Key_1) Then
TestBall.Add
FlushKeys
EndIf
If KeyDown(Key_2) Then
If CountList(BallList) > 0 Then
For MrBall = EachIn BallList
'Sloppy way to get last Ball
Next
MrBall.Remove
FlushKeys
EndIf
EndIf
MouseXSpeed() ;' flush
MouseYSpeed() ;' flush
;' move camera forwards/backwards/left/right with cursor keys
If KeyDown(KEY_UP)=True Then MoveEntity cam,0,0,move# ;' move camera forward
If KeyDown(KEY_DOWN)=True Then MoveEntity cam,0,0,-move# ;' move camera back
If KeyDown(KEY_LEFT)=True Then MoveEntity cam,-move#,0,0 ;' move camera left
If KeyDown(KEY_RIGHT)=True Then MoveEntity cam,move#,0,0 ;' move camera right
For MrBall = EachIn BallList
MrBall.Spin
Next
RenderWorld
renders=renders+1
;' calculate fps
If MilliSecs()-old_ms>=1000
old_ms=MilliSecs()
fps=renders
renders=0
EndIf
Text 0,0,"FPS: "+fps
Text 0,15,"Mem in Use: " + ((GCMemAlloced()) / 1000) + " KBs"
Text 0,30,"Balls in World: "+CountList(BallList)
Text 0,60,"Press 1 to Add a Ball"
Text 0,75,"Press 2 to Remove a Ball"
Flip
Wend
End
Type TestBall
Field BallEntity
Function Add()
Local obj:TestBall
obj:TestBall = New TestBall
obj.BallEntity=CreateSphere(24)
PositionEntity obj.BallEntity,CountList(BallList)*3,0,0
EntityTexture obj.BallEntity,tex
ListAddLast(BallList,obj:TestBall)
End Function
Method Remove()
FreeEntity BallEntity
BallList.Remove Self
End Method
Method Spin()
TurnEntity BallEntity,0.2,1,0.1
End Method
End Type
|
| ||
try this:Type TestBall Field BallEntity:Tentity It is always good practice to explicitly type the objects. The Freeentity command is looking for a Tentity. I would *highly* recommend using strict or superstrict while coding in blitzmax. It forces you to declare stuff but makes for much cleaner and easier to debug code. Here is your example using strict. freeentity now removes the mesh and frees the memory. (I commented out the texture stuff so i could run it without your media, don't really need it for this test) |
| ||
| Thank you. I'm feeling a little stupid but this clarifies things... I'm gonna start using strict from now on. |
| ||
| don't feel that way. when i started with max, I never used strict. I hated the idea of all that extra typing! What I found, however, is that my code is MUCH easier to read and I find bugs more easily. it is so easy to do a for i = 1 to max_lnes instead of max_lines or something and without strict, it will just silently fail. anyway, welcome aboard! |
| ||
| I try to think of strict and superstrict (which I always use) as sort of an inline spell checker. Rather than type everything out, hit go and nothing happens (or worse...) it makes the IDE sanity check what I wrote for the painfully obvious mistakes that I'm very prone to make (suuch aas typoos inn vairieble naames) which cause cold sweats and nightmares while trying to figure out why something doesn't work. Switching between data types on the fly gets anoying since blitz won't always translate one type to another with suprstrict, but the upside of that is if you ever want to take your code, or a fragment of your code into another language it will work with only changeing a little syntax, atleast for most math heavy stuff, which is the stuff I personaly don't want to have to figure out all over again when I share things between projects and with friends. |