GMS 2 Efficient way to destroy many lists?

Discussion in 'Programming' started by Solitary Confinement, Oct 10, 2019.

  1. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    29
    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!
     
  2. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,207
    Code:
    for(var a = 0; a < 1000; a++)
        list[a] = ds_list_create();
    
    for(var a = 0; a < 1000; a++)
        ds_list_destroy(list[a]);
    
    Just created and destroyed 1000 lists for ya :)
     
  3. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,903
  4. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,447
    Or even a ds list that holds all the other list!
     
    Solitary Confinement and Catan like this.
  5. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    804
    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.
     
    Solitary Confinement likes this.
  6. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    29
    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: Oct 10, 2019
  7. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,903
    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.

    :)
     
    Solitary Confinement likes this.
  8. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    29
    ^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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice