[SOLVED] cleaning temp data structure ?

MCHLV

Member
Hello all,

Quick question, but I was not able to solve this...

In a script like
Code:
/// include_item_in_master_map()
var _item_name       = argument0;
var _item = ds_map_create();
_item[? "attribute"]    = argument1;
_item[? "power"]       = argument2;
ds_map_add(master_map, _item_name, _item); 
ds_map_destroy(_item);
Is the last line 'ds_map_destroy(_item)' required for cleansing ?
I read about the need to clean data structure because the index is incremented but do I need to do it for a temp/local variable in a script ??
Thanks for your confirmation.
M.
 

CloseRange

Member
Yes it is very important to always clean data structures. Even if you remove reference to them they still exist in memory.
When you use something like var _item the reference is removed after the script ends. However that map is still in memory until you call item destory or when the program ends and the OS garbage collects it.
Not clearing it could result in memory leak down the line.
 

TsukaYuriko

☄️
Forum Staff
Moderator
There's an important distinction to make here: ds_map_destroy destroys a ds_map, not a variable.
Local variables remove themselves from memory after a code block ends.
Data structures remain in memory until deleted. <- this is the one ds_map_destroy destroys.

Do you still need the map after the script finishes?
If yes: Don't destroy it.
If no: Destroy it.

From what I can tell, you're creating a map and adding it to another map. Said other map presumably will be using the map you created even after the script finishes.

If you destroy the map at the end of that script, what you'll have added to your master map is the ID of a map, but that map won't actually exist - which would not only make running the script pointless (as it generates data that can not be used because it instantly destroys it), but may very well lead to unexpected behavior (if you create another map that has the same ID assigned, but contains wrong data) or crashes (if no map with that ID exists when you try to access it) further down the line.

So actually no, do not destroy it in this case.

If this is, for example, a consumable item that is removed from your inventory upon being used, that is when you should destroy the map.
 
Top