Neccacry to destroy ds_stacks that are local variables?

R

Robin Gherghetta

Guest
If I create a stack as a local variable:
var stack = ds_stack_create()

Do I still have to destroy it with ds_stack_destroy(stack)? What if i use it in a script and then return it? How can i destroy it after I return it in the script as anything after the return will not execute?

....
return stack;
//this will not execute
ds_stack_destroy(stack);

Same question goes for ds_maps and ds_list. I read in the manual:

NOTE: As with all dynamic resources, data structures take up memory and so should always be destroyed when no longer needed to prevent memory leaks which will slow down and eventually crash your game.

"Always" is even bolded!
 

Paskaler

Member
You must delete all ds_* before you lose a reference to them as that will cause them to leak.

So, yes. Delete them all as soon as you don't need them.

The same goes for instances, surface buffers and some more: can't remember th all now.
 

FrostyCat

Redemption Seeker
If I create a stack as a local variable:
var stack = ds_stack_create()

Do I still have to destroy it with ds_stack_destroy(stack)?
Yes. Functions that create data structures return an ID reference to the data structure, not the data structure itself. Here var makes the ID reference temporary, it doesn't make the data structure itself temporary.

What if i use it in a script and then return it?
You destroy it if nothing else should or need to hold the ID to that stack afterwards.

If it is just a temporary utility for computation within the script, nothing past the return should hold that ID. That's your cue to destroy it there and then.

If it is returned from a script, whoever calling that script should hold that ID. That's your cue not to destroy it yet.

How can i destroy it after I return it in the script as anything after the return will not execute?
Then it's the job of whoever calling that script to destroy the returned stack, leave the script out of it.

Topics like this just reinforce my belief that programmers aren't complete until they have experience in a non-GC language.
 

Yal

šŸ§ *penguin noises*
GMC Elder
What if i use it in a script and then return it? How can i destroy it after I return it in the script as anything after the return will not execute?
If you get the ID of the stack as the return value, have whatever called the script be responsible for destroying the stack? If you don't return the stack, destroy it before returning (but after getting whatever stuff you need out of the stack as normal variables).
 
K

kevins_office

Guest
Think of it this way...
When you make a local Real or String variable like "var myVar = 1;" then myVar is the actual variable.
But when you do "var myVar = ds_list_create();" then myVar is NOT the variable, it is just a handle/reference to the data structure under the hood.
When the event/script ends and looses scope, then myVar is discarded/destroyed.
If myVar was the actual variable "var myVar = 1;" then the actual variable is destroyed.
But if myVar was only a handle to a data structure then still only myVar (the handle/reference, not a variable) is destroyed but not the data structure it was linked to.
 
R

Robin Gherghetta

Guest
So the returned stack really is a pointer to the original stack, and not a copy, and so therefore I deal with it like this?

var available_moves = scr_available_moves() //returns stack
while( !ds_stack_empty(available_moves()) ) {
var move = ds_stack_pop();
.....
}
ds_stack_destroy(available_moves)

Thanks for the help!
 
Top