GameMaker Efficient way to destroy many lists?

Heya,

I understand one needs to ensure a ds_list is destroyed once it is no longer needed to avoid memory leaks. What's an efficient way to do this?

If I have the following (just an example, actual project has many more):

list_a=ds_list_create();
list_b=ds_list_create();
list_c=ds_list_create();

Would I need to then have, in room end event:

ds_list_destroy(list_a)
ds_list_destroy(list_b)
ds_list_destroy(list_c)

Surely there is a better way to destroy the lists? I tried destroying the lists by creating a master "destroy" list and adding all the above to that list. On room end, I then used: ds_list_destroy(master_destroy_list[| ds_list_size(master_destroy_list)]), but this threw an error at me.

Any guidance is appreciated. Thanks!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin

GMWolf

aka fel666
As an alternative to the array procedure mentioned above, you could make a DS map that holds each list as individual map entries, adding the lists to the map using the function ds_map_add_list(). You would then only have to destroy the parent DS map to also clean up all the lists it contains. https://docs2.yoyogames.com/index.html?page=source/_build/3_scripting/4_gml_reference/data_structures/ds maps/ds_map_add_list.html
Or even a ds list that holds all the other list!
 

Bentley

Member
Don't have GM open but something like this?
Code:
var i = 0;
while (ds_exists(ds_type_list, i))
{
    ds_list_destroy(i);
    i++;
}
Edit: maybe that throws an error when I gets 1 to big.
 
Thank you all.

Just a couple things though:

@Lonewolff

I'm not sure I'm doing this correctly and that the list is actually being destroyed. Here is what I have:

Create:

Code:
//After lists abc and def already made
list_master_destroy=ds_list_create();

ds_list_add(list_master_destroy,
list_abc,
list_def
)
Key press event (for testing):
Code:
for(var a = 0; a < ds_list_size(list_master_destroy); a++)
    list[a] = ds_list_create();
     
    for(var a = 0; a < ds_list_size(list_master_destroy); a++)
    ds_list_destroy(list[a]);
When I press the key, nothing happens, so I assume it works. But I'm also drawing the lists that need to be destroyed, as follows:

Code:
if ds_exists(list_abc,ds_type_list)
        {
        for(var i=0; i<ds_list_size(list_abc); i++)
            draw_text(1400, 105 + (24*i), string(list_abc[| i]));
        }
Upon key press, the contents of list_abc can still be seen. Should I not get an error telling me that the list doesn't exist or something similar? That, and that I didn't see where examples of list_abc and list_def were being added to the "list" variable in your example.

@Nocturne

The manual doesn't highlight this, but is it the case that for multiple list entries, one would have to use the ds_map_add_list function for each and every list entry, as follows:

Code:
ds_map_add_list(map_master_destroy,"1",list_abc);
ds_map_add_list(map_master_destroy,"2",list_def);
Thanks again to everyone for your input!
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
The manual doesn't highlight this, but is it the case that for multiple list entries, one would have to use the ds_map_add_list function for each and every list entry, as follows:
Yes that is the case... so, the process would be:

you make X number of lists.
you add these lists to the master DS map, using the ds_map_add_list function for each one
when required, call ds_map_destroy and the map will be removed from memory along with all the lists it contains.

:)
 
^Thanks for confirming!

One last question regarding something I just realized: will calling the ds_map_destroy function remove newly created lists which have been created after destroying the original, but then copied and shuffled in order to "renew" the list, and which have the same name? For example, I'm copying list_abc_main from list_abc_source. To renew abc_main, once its entries have been exhausted (via ds_list_delete) I destroy abc_main, create a new abc_main, then copy its contents from abc_source, then shuffle it.

I'm assuming one would have to use "ds_map_add_list(map_master_destroy,"1",list_abc_main);" right after the old one is destroyed and the new one is created. But if this isn't actually the case, it'd really help to not have to do this twice for each list - once on create and once during the renewal process.

Thanks again!
 
Top