GMS 2 Need help destroying data structures

Hello. Sad to say that I haven't been destroying my data structures, and now that I'm going back through my code to do so, I'm getting all kinds of "index does not exist" errors.

I just need to clear up the confusion in my head about how to do this properly.

From the manual, I understand that you should 1) check the DS exists, 2) destroy it, and 3) set the variable that held it to -1.

But in my case, I'm passing these data structures from script to script. Let me try to make a simple example here:
// script 1

var list_of_maps_2 = scr_script2();

// script 2

var list_of_maps_1 = ds_list_create();

for (i = 0; i < 3, i++)
    var map = ds_map_create();
    // put stuff in map
    ds_list_add(list_of_maps_1, map);

return list_of_maps_1;
To me, it looks like I now have 8 data structures in that example (2 lists, each containing 3 maps). OR is one list just pointing to the other? This is where I get confused. And I can't go about destroying them until I understand this. Thank you for any advice.


You have one list, holding three maps. list_of_maps_1 and list_of_maps_2 are the same list.

EDIT: To be a little more useful in a general sense, every data structure should, by design, have an owner. A single instance which is responsible for tracking that data structure and destroying it when it's no longer needed. When the list is maintained entirely within an instance, that's easy. Create it in the Create event, destroy it in the Cleanup event. But when you use scripts like this, it can get a little more complicated. You have a few basic possibilities:
  1. The script creates the list, uses the list, then destroys the list. This is easy - the script owns the list, and nobody needs to track it at all.
  2. The list is passed into the script, manipulated, and the script ends. Whoever called the script owns the list, and you don't need to worry.
  3. The list is created in the script and then returned. Now, whoever called the script has to take ownership of the list and know when to destroy it. That's for you to design.
  4. The list is passed into the script and destroyed within the script. This is a rare case, but it's essentially a destructor script for the list. The owner would call that script when it knows the list should be destroyed.
Last edited:


You have just 1 list and 3 maps. The one being returned and the one in list_of_maps_2 are one and the same.

When you return list_of_maps_1, you are not returning the list itself, but something resembling a phone number for that list. In the same way, the list is holding the equivalent of phone numbers for maps, not the actual maps. The same mechanics apply to any other data structure, buffer, or any resource in GM that talks about using index numbers.

Here are several consequences of index-accessed resources that you must be aware of:
  • You cannot tell what kind of entity is on the other side of the phone number, it's all a number.
  • You can copy a phone number several times, but in the end they all point back to the same person, you don't end up with clones of that person.
  • Deleting a phone number from your phone book does not kill the person it belongs to.
  • If you lose track of the phone number, that person still exists, you just have no way of phoning that person.
  • If that person dies, records of that phone number can still be around.
  • If someone calls a phone number that belonged to a dead person, either nobody picks up, or somebody else who got reassigned the same number picks up.
On an unrelated note, you have an i variable that has escaped into instance scope. Big no-no.