When should I destroy a list?



I'm confused about when I should destroy the list? Do I need to do it everytime I change to a new room? or when the object is no longer needed?


It's more about when the data is not needed. If you're just using the list to sort something/manipulate the data and you don't need it afterwards then you can destroy it.

If you're using the list to store data that you need to access throughout your game then you won't destroy it until the game closes.


Hmm, good question though.
If the list isn't global and the instance isn't persistent, shouldn't you have to destroy it when you move to a different room? I'd expect it to be a memory leak.


If you move rooms, you should destroy it (especially if it gets recreated) unless it is in an object that is persistent... habit makes me destroy them all and recreate in next room (better safe than sorry) TBH.

A lot of people run into issues because they have a ds_list_create() in a repeating event, and think because they are using the same variable name it just overwrites it... it doesn't, it makes a new one... instead create the list once and use ds_list_clear or whatnot if you need the ds_ again.

For example, I use a global priority Q that is checked for new entries every step in a particular object for the whole game for a turn-based strategy. Its job is to kick off a particular set of events (in order of course lol) when they are triggers by an instance. I never get a memory leak. If I was changing rooms, I destroy it and remake it.