Flushmem may not be working?
BlitzMax Forums/BlitzMax Programming/Flushmem may not be working?
| ||
I have this little program: ------------------------------------------------------ Strict 'STANDAR TYPE FOR ALL GRAPHICAL OBJECTS: Type Base Abstract Global Elements:Int Field X:Float Field Y:Float Method New() 'EVERYTIME A GRAPHICAL OBJECT IS CREATED; IT IS COUNTED: Elements:+1 End Method End Type 'EVERY STAR IS AN OBJECT: Type Star Extends Base 'IT EXTENDS BASE BECOUSE IT IS A GRAPHICAL OBJECT Field Speed:Float 'EVERY STAR HAS ITS OWN SPEED Field Parent:Sky 'EVERY STAR IS ALLOCATED INSIDE AN SKY OBJECT Field ColorIntensity:Int Method New() Speed = Rand(1001,1020)/1000.0 'WHEN A STAR IS CREATED, A RANDOM SPEED IS ASSIGNED TO IT ColorIntensity = Rand(105,255) x=Rand(0,GraphicsWidth()) 'IT IS PLACED ON THE SCREEN y=Rand(0,GraphicsHeight()) End Method Method Move() 'THIS IS CALLED TO MOVE STARS. Local OldX,OldY OldX = X OldY = Y ' self.x=(self.x-GraphicsWidth()/2)*Speed+GraphicsWidth()/2' ' self.y=(self.y-GraphicsHeight()/2)*Speed+GraphicsHeight()/2 self.x=(self.x-MouseX())*Speed+MouseX() self.y=(self.y-MouseY())*Speed+MouseY() 'IF A STAR IS OUT OF BOUNDS, IT IS DESTROYED If ((self.x<0) Or (self.x>GraphicsWidth()) Or (self.y<0) Or (self.y>GraphicsHeight())) parent.DeleteStar(Self) Else SetColor(ColorIntensity,ColorIntensity,ColorIntensity) DrawLine(X,Y,OldX,OldY) 'We draw the star EndIf End Method End Type Type Sky Extends base Field Stars: TList 'A LIST OF ALL STARS INSIDE THE SKY Field StarNum:Int 'THE NUMBER OF STARS THE SKY OBJECT WILL OWN Method New() Stars = New TList 'START DE STARS COLLECTION Local i:Int=0 Starnum = 500 For i=1 To Starnum addstar() Next End Method Method AddStar() Local I:Star=New Star 'WE CREATE A NEW STAR i.parent = Self 'WE TELL THE NEW STAR THIS SKY IS ITS PARENT Stars.AddLast I 'WE ADD THE NEW STAR IN THE STARS LIST. End Method Method Update() Local Counter:Int = 0 'STAR COUNTER Local j:Int=0 'FOR - NEXT INDEX Local i:star 'STAR TYPE DECLARATION For i:star= EachIn stars 'WE GO THROUG ALL STARS LIST, MOVING EVERY STAR. Counter:+1 i.move() Next i = Null ' WE FREE THE I VARIABLE AND ITS STAR OBJECT. For j = counter To starnum 'WE CREATE AS MUCH STARS AS NEEDED TO REPOSE THE ONES WHO HAVE DIED. addstar Next End Method Method DeleteStar(StarToDelete:Star) Stars.remove (StarToDelete) 'WE REMOVE THE STAR FROM THE COLLECTION Elements:-1 'UPDATE ELEMENTS COUNTER (ELEMENTS IS INSIDE BASE TYPE DECLARATION AND IT IS ACCESIBLE THROUG ALL OBJECTS THAT EXTEND BASE). End Method End Type Graphics 1024,768 HideMouse Local mysky:sky=New sky Local FrameTimer:Float=MilliSecs() While Not KeyHit(27) Cls FlushMem mysky.update() DrawOval(MouseX()-5,MouseY()-5,10,10) 'DrawText MemAlloced(), 1,1 While MilliSecs () < FrameTimer + 35 Wend FrameTimer = MilliSecs () Flip Wend EndGraphics '--------------- If i put the FlushMem inside the method Update of the object SKY, FlushMem doesn't do anything. I don't know wht, but it just doesn't do anything, and memAlloced() grows ad grows non stop, but when I put ir in the main loop (where it is now) it works ok. My question is: WHY? |
| ||
Flushmem needs to be in the mainloop as it releases memory from any 'child' loop. In your program it is only releasing memory freed within that method. There is a thread somewhere which explains it (in the Bug Reports I believe). Here Why the hell can't I get this chuffing anchor tags to work??? <edit> FINALLY!!! |
| ||
If i put the FlushMem inside the method Update of the object SKY, FlushMem doesn't do anything. Flushmem only works within the confines of its respective scope.Thus a Flushmen inside a function or method will only free resources allocated within that function or method, or within functions or methods invoked from that scope. |
| ||
> Flushmem only works within the confines of its respective scope. I hope that means calling it globally will handle anything lower level that was called. Locals within a procedure for example. (I'm sure this is mentioned in the docs, but it would be an easy oops to make) |
| ||
Dynaman, anything lower is handled. The only reason you may need additional flushmems is if you function does a LOT of work before passing control back to the main loop for the flushmem. Hope that makes sense. |
| ||
Yup - makes sense, it's what I would expect anyway... |