GMS 2 Does destroying a data structure destroy data structures within it?

The manual says that destroying a data structure removes all values that it contains, but does that include other data structures?

If, for example, I have a stack of maps, does ds_stack_destroy() remove all those maps, too?

My memory usage grows with every room_restart() so I'm trying to identify the cause. Thanks.
However, be wary of deleting positions with a data structure. For instance, destroying a list which has a few maps in it that are marked as maps will also destroy those maps which is totally understandable. However, let's say you have a list and position 0 contains a map (and is correctly marked as a map, yadda yadda yadda). If you DELETE position 0 in the list, instead of destroying the whole list, the map won't actually get deleted alongside the position in the list and, if you have no other pointers to that map's ID, it will be dereferenced and you'll have a memory leak. So if you're deleting positions that contain a DS, you'll have to manually destroy the DS alongside the deletion (usually by destroying the internal DS first using the index stored in the position and only then deleting the position itself).


Forum Staff
@TsukaYuriko, so if it isn't being used with JSON, then I should extract each data structure and destroy them one by one. Is that correct?
Not necessarily if it isn't, but if it can't. ;)
The JSON functions mark decoded lists and maps as such, which is what causes cascading destruction when you destroy the top one.

You can mark stuff like this by yourself as well, so if you do that, destroying the top one will also cascade.


Throwing away a phonebook doesn't kill anyone cited inside it. Have you already forgotten the lesson?

The JSON marking functions are the only exceptions. They internally indicate the type of data structure behind the index number, and also instruct the runner to recursively remove nested structures when that entry is deleted, or when the list/map containing that entry is cleared or destroyed.
and also instruct the runner to recursively remove nested structures when that entry is deleted
@FrostyCat Pretty sure deleting individual list positions does not delete a corresponding DS stored in that position, only destroying the list as a whole will. As a test, I set up two conditions, one where the list entries are being deleted when I click and one where the lists themselves are being destroyed when I click. List 1 has 3 maps stored and marked as maps inside it. I delete the list positions of list 1 individually and you can see the total number of maps doesn't change. I then set it to destroy and destroy the lists (which obviously doesn't destroy the maps because the positions inside the list 1 have been deleted already). Then I reset everything and instead of deleting the list positions, I destroy the lists straight up and you can see the number of maps DOES change then:

I ran into a bit of trouble with this when I was working on Floramancer, so I'm pretty sure that marking DS' doesn't force them to be destroyed when their position is deleted, only when the entire DS' containing them gets destroyed.


I had the same experience as @RefresherTowel , only deleting or clearing the whole thing destroys nested (and marked) structures.
These cases are poorly documented imho, I opened a ticket a while ago asking to go into a bit more detail about what happens when replacing or deleting marked items, but I haven't seen any relevant changes so far.
I mean, it makes sense in a roundabout kind of way not to destroy DS' stored in a position when the position is deleted. For example, let's say you have an inventory list, with each position being a map storing some data about the item. You then want to, I dunno, show all the armour in your inventory. You create a temporary list, loop through the inventory and store the list references for all the items that are armour in your temporary list. The player can select stuff from this armour list, so when they click on the armour item, you delete the position from the list. If that destroyed the armour ds_map, you'd lose the armour from the inventory (which, in essence, would force you to clone your inventory list AND create a temporary list to get this kind of thing going, or clone each map, or some other vaguely more roundabout way of doing it). But because it only deletes the list position and doesn't destroy the map, your inventory list is safe.

However, very much yes, the documentation is sorely lacking in some of this more obscure functionality and the number of hours I've wasted trying to hunt down a bug only to later be told by someone in the know "It functions in x way but that's undocumented" is pretty obscene.