1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

[SOLVED] cleaning temp data structure ?

Discussion in 'Programming' started by MCHLV, Jan 24, 2020.

  1. MCHLV

    MCHLV Member

    Sep 5, 2016
    Hello all,

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

    In a script like
    /// 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); 
    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.
  2. CloseRange

    CloseRange Member

    Jul 2, 2016
    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.
  3. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Apr 21, 2016
    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.
    IndianaBones and ThePC007 like this.
  4. MCHLV

    MCHLV Member

    Sep 5, 2016
    Thank you both @TsukaYuriko , @CloseRange ,

    @TsukaYuriko, this make sense now. The id / The DStructure and how they are managed. Thank you very much !
    I use this to build the dictionary of all existing items. So the DS should only be destroyed on game end.


Share This Page