Need to free memory?

Previous topic - Next topic

Dark Schneider

Hi, my question is if we need to free memory of anything other than resources (sprites, sounds), i.e. if we have:

Code (glbasic) Select

TYPE TMyType
   array[]

FUNCTION Init:
   DIM self.array[x]
ENDFUNCTION
ENDTYPE

FUNCTION MyFunc:
LOCAL myvar[] as TMyType
DIM myvar[x]
FOREACH item IN myvar[]
   item.Init() // this does the DIM array[x] in the TYPE
NEXT

// SOME CODE HERE...

//There is need to do this, or we can forget about all memory management?
FOREACH item IN myvar[]
   DIM item.array[0]
NEXT
DIM myvar[0]
ENDFUNCTION


In other words, we need to worry about arrays?

Moru

Short answer: No, not usually.

Longer answer: If you make really big arrays and need the memory yourself you might want to read up on the command REDIM. It can resize an array and keep the allocated memory so you don't have to allocate it again. Reallocating memory can be very time-consuming for the computer. I haven't had much chance to program GLBasic lately so I can't remember exactly how it worked though :-)

Dark Schneider

Interesting, then I will change the question a bit: GLBasic frees arrays automatically when they're no more needed?.

It is important to know exactly to evade out of memory on devices.

Kitty Hello

no - memory is freed only, if you DIM array[0], or the array variable goes out of scope.

Dark Schneider

#4
Quote from: Kitty Hello on 2010-Nov-30
or the array variable goes out of scope.

So, if we do:

Code (glbasic) Select

TYPE TMyType
   array[]

FUNCTION Init:
   DIM self.array[x]
ENDFUNCTION
ENDTYPE

FUNCTION MyFunc:
LOCAL myvar[] as TMyType
DIM myvar[x]
FOREACH item IN myvar[]
   item.Init() // this does the DIM array[x] in the TYPE
NEXT

// SOME CODE HERE...

//Free
DIM myvar[0]
ENDFUNCTION


Are the arrays of types freed?.

MrTAToad

Arrays of types local to the function are freed when the function ends