Marking data structures in ds_maps

ophelius

Member
When adding ds_lists or ds_maps to an existing ds_list, you can mark them as such using ds_list_mark_as_list() and ds_list_mark_as_map(), so that when you clear the parent list, it also deallocates those added data structures from memory.

But I'm trying to figure out how to mark a data structure added to a ds_map, there's no such function. This is causing memory leaks, and clearing the parent ds_map doesn't deallocate those added data structures.

How would I go about making sure that any ds_lists or ds_maps added to a parent ds_map is fully cleared without memory leaks? Thanks
 

Roldy

Member
When adding ds_lists or ds_maps to an existing ds_list, you can mark them as such using ds_list_mark_as_list() and ds_list_mark_as_map(), so that when you clear the parent list, it also deallocates those added data structures from memory.

But I'm trying to figure out how to mark a data structure added to a ds_map, there's no such function. This is causing memory leaks, and clearing the parent ds_map doesn't deallocate those added data structures.

How would I go about making sure that any ds_lists or ds_maps added to a parent ds_map is fully cleared without memory leaks? Thanks
They probably don't exist for map.

If you read the ds_list page it states those *_mark_as_list() functions where created for JSON encoding. Even though they work outside of json_encode(), however the map ones do not.

Look at the bottom of the map page in the JSON section:

Here the map has similar functionality. But the manual specifically states they only work with the json_encode routines.

NOTE: While these functions permit you to add lists and maps within a map, they are useless for anything other than JSON, and nested maps and lists will not be read correctly if written to disk or accessed in any other way.
 

ophelius

Member
While these functions permit you to add lists and maps within a map, they are useless for anything other than JSON, and nested maps and lists will not be read correctly if written to disk or accessed in any other way.
The lists and maps are added to a map, which is essentially a json structure is it not? Everything I'm adding to my ds_map, wether it's other lists or other maps, my game still works, I'm just noticing small memory leaks after clearing that main map.

When I use ds_map_add_list() to add a list to a ds_map, I can't clear the map without causing memory leaks because those lists I added are still in memory after the clear. What is the solution?
 

chamaeleon

Member
The lists and maps are added to a map, which is essentially a json structure is it not? Everything I'm adding to my ds_map, wether it's other lists or other maps, my game still works, I'm just noticing small memory leaks after clearing that main map.

When I use ds_map_add_list() to add a list to a ds_map, I can't clear the map without causing memory leaks because those lists I added are still in memory after the clear. What is the solution?
Unless I'm mistaken, the whole point of the ds_map_add_list and ds_map_add_map functions is to exactly allow the containing map to free the stored structures because it is the owner and not just a reference to them. If you can write a tiny example that exhibit the memory leak either your code or GMS has a bug.
 

samspade

Member
@chamaeleon is right. ds_map_add_list and ds_map_add_map are the map counterparts to ds_list_mark_as_list and ds_list_mark_as_map. Personally, I think those are the more useful versions. I use them regularly and have no issues.

When I use ds_map_add_list() to add a list to a ds_map, I can't clear the map without causing memory leaks because those lists I added are still in memory after the clear. What is the solution?
I'm not sure I understand this, clearing a ds_map will destroy nested maps and lists if they have been added using the above functions. Quoting from the manual: "The only time this is not required is when you have flagged any items in the map as a DS list or as another DS map, in which case these items will be destroyed (not cleared!) and their memory cleaned up automatically when the parent map is cleared." This is something I have done in several projects and never seen any issues with.
 

ophelius

Member
Ok thank you guys, the leak must be coming from somewhere else then.
It's this part that confused me from the manual that samspade mentioned, I also read it too: "The only time this is not required is when you have flagged any items in the map as a DS list or as another DS map..". That sounds like there's a way to flag a list or map in a ds_map, but appently not.
It's good to know that ds_map_add_list and ds_map_add_map don't require me to flag anything like ds_lists. That'll help my search. Thanks
 

chamaeleon

Member
Ok thank you guys, the leak must be coming from somewhere else then.
It's this part that confused me from the manual that samspade mentioned, I also read it too: "The only time this is not required is when you have flagged any items in the map as a DS list or as another DS map..". That sounds like there's a way to flag a list or map in a ds_map, but appently not.
It's good to know that ds_map_add_list and ds_map_add_map don't require me to flag anything like ds_lists. That'll help my search. Thanks
The flagging is implicit when you use the ds_map_add_list and ds_map_add_map functions. It is explicit for ds_lists because you can't provide the information in the the ds_list_add call (only the value to be stored is passed which does not carry the information in itself, as the data structures are just referenced as numbers), so you need to mark it after the fact. Yes, YYG, could have added ds_list functions for adding a single item to the list that works like the functions for ds_map, but they haven't done so.
 

ophelius

Member
The flagging is implicit when you use the ds_map_add_list and ds_map_add_map functions. It is explicit for ds_lists because you can't provide the information in the ds_list_add call (only the value to be stored is passed which does not carry the information in itself, as the data structures are just referenced as numbers), so you need to mark it after the fact. Yes, YYG, could have added ds_list functions for adding a single item to the list that works like the functions for ds_map, but they haven't done so.
Thank you, that helps me understand the inner workings a bit better.
 
Top