Hi a i have a little question:
here is an example code:
TYPE TMap
position AS TVec2d
width
height
layers[] AS TLayer
FUNCTION Create:
DIM self.layers[10]
FOREACH layer IN self.layers[]
layer.Create()
NEXT
ENDFUNCTION
FUNCTION Destroy:
FOREACH layer IN self.layers[]
layer.Destroy()
NEXT
DIM self.layers[0]
ENDFUNCTION
ENDTYPE // TMap
//////////////////////////////////////
TYPE TLayer
position AS TVec2d
entities[] AS TEntity
isVisible
FUNCTION Create:
self.Destroy()
ENDFUNCTION
FUNCTION Destroy:
DIM self.entities[0]
ENDFUNCTION
FUNCTION AddEntity: entity AS TEntity
DIMPUSH self.entities[], entity
ENDFUNCTION
ENDTYPE // TLayer
and now is the question:
is function Destroy in TYPE TMap shoud be:
FUNCTION Destroy:
FOREACH layer IN self.layers[]
layer.Destroy()
NEXT
DIM self.layers[0]
ENDFUNCTION
or
FUNCTION Destroy:
DIM self.layers[0]
ENDFUNCTION
is good enought ??
regards
For dealloc array from memory you have to use dim array_Name[0]... If I don“t remenber bad, if you do dimpush, only delete the index but the array continue in memory...
Technically, using DIM xxx[0] should de-allocate everything, although it may only happen when the computer has enough free time to do so.
However... if you have been using files, loading specific graphics/music etc that you want to have removed when the type is destroyed, then you would need the FOR/NEXT loop to close file handles and so on.
You may also like to make sure that you always deallocate arrays in your Destroy function anyway to maintain symmetry with the your constructor and to make sure everything does get de-allocated.
I agree with Mr. T. "Destroy", is the better of the two. Both will work but Destroy(1st method) gives you a more finite method destruction than just removing stuff from memory. Changing global counters, informing other sub types of the destroy event etc.
Killing each layer makes sense, certainly for insurance of being cleared.
If the list is massive it might take more time to complete the function.
Some test cases:
If the typical case of use is in fact a small array, regardless of useage occurances, then the first case should be sufficient.
In a case where the array might contain massive amounts of data, and you want your program to run as fast as possible, then the second case might be more desireable.
Thanks for replies ...
Cheers
No problem!
But technically dim cxx]0] also frees all child item arrays.