Jump to content


Photo

Json_decode and cleaning up leaks


  • Please log in to reply
8 replies to this topic

#1 Sulsay

Sulsay

    GMC Member

  • GMC Member
  • 256 posts
  • Version:GM:Studio

Posted 20 December 2012 - 08:22 AM

Hi, just a quick question about json_decode as I'm not quite sure how to properly handle it.

The manual clearly states that ds_maps and ds_lists get created for the obejcts and (nested) arrays in the json. And also that cleaning up is the responsibility of the developer.

My question is, do I need to cleanup every potentially created ds_map and ds_list, even if I didn't use them after calling json_decode? Imagine I use only a part of the json (say, the first object in an array, but not the others), do I need to clean up everything or only the used part?

I'll write a cleanup function if need be, just wanted to be sture =)

Edited by Sulsay, 20 December 2012 - 08:22 AM.

  • 0

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 22525 posts
  • Version:GM:Studio

Posted 20 December 2012 - 09:08 AM

You need to destroy everything. If the ds_map returned contains five ds_lists, even if you only use the information from one of them, the other four still exists and so you should clean up everything.
  • 0

#3 Sulsay

Sulsay

    GMC Member

  • GMC Member
  • 256 posts
  • Version:GM:Studio

Posted 20 December 2012 - 09:13 AM

Alright, good to know. Thanks for your quick reply!
  • 0

#4 Sulsay

Sulsay

    GMC Member

  • GMC Member
  • 256 posts
  • Version:GM:Studio

Posted 21 December 2012 - 12:26 PM

Sorry for the extra post, but I have an additional question.

Memory management after decoding a json structure appears to be quite challenging. Imagine this json:

[
  {
    "a": "one",
    "b": [ 0, 1, 2, 3 ]
  },
  {
    "a": "two",
    "b": [ 4, 5, 6, 7 ]
  }
]

I loop through all objects in the array.
Every object has a property "b" that holds an array.

First I copy the structure into a new ds_map/list, then I destroy the original ds_map/list.
When I'm done, I destroy the copy.

Then, I have a cleanup method (script) to handle everything that I didn't need. However I can't detect which ds_structures still exist, there's no function for this.

Is there a recommended way for dealing with these structures? Debugging is hard with data structures.
  • 0

#5 gnysek

gnysek

    GMC Member

  • GMC Member
  • 552 posts
  • Version:GM:Studio

Posted 21 December 2012 - 02:32 PM

I think it should be ok...

var _start, _end, _i;
_start = json_decode('string here');
_end = ds_map_create();
//your code

for(_i=start; _i<=_end; _i++) {
ds_map_destroy(_i);
}

Edited by gnysek, 21 December 2012 - 02:33 PM.

  • 0

#6 Sulsay

Sulsay

    GMC Member

  • GMC Member
  • 256 posts
  • Version:GM:Studio

Posted 24 December 2012 - 09:57 AM

But if I was to destroy all the maps, wouldn't that leave the inner lists in-memory, lost somewhere? (these would be "b" keys in the json above).
  • 0

#7 gnysek

gnysek

    GMC Member

  • GMC Member
  • 552 posts
  • Version:GM:Studio

Posted 28 December 2012 - 11:36 AM

Every time that ds_map is created, it gets as ID next integer number. So in my code first ID is assigned to _start, then inner ds_maps are created, then there's additional ds_map ID assigned to _end - because we don't know how many inner maps was created, but we knows, that next map will get next ID.
So between _start and _end integers there will be a gap with numbers - those are inner ds_maps :)
  • 0

#8 Sulsay

Sulsay

    GMC Member

  • GMC Member
  • 256 posts
  • Version:GM:Studio

Posted 28 December 2012 - 12:13 PM

Oh, that's brilliant! Really clever solution!

And I know I can easily test this myself but I'm curious for the answer; when mixing ds_maps and ds_lists, are the IDs sequential as well? Or does every ds-type have its own stack of IDs? Cleaning up the lists as well would be quite easy if the latter is the case.
  • 0

#9 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 4485 posts
  • Version:GM:Studio

Posted 01 January 2013 - 03:51 AM

And I know I can easily test this myself but I'm curious for the answer; when mixing ds_maps and ds_lists, are the IDs sequential as well? Or does every ds-type have its own stack of IDs? Cleaning up the lists as well would be quite easy if the latter is the case.

From my experimentation, each type maintains its own stack of IDs. Consider the following code:
{
    var i, ds;
    for (i=0; i<20; i+=1) {
        if (i mod 3 == 0) {
            ds = ds_list_create();
            show_debug_message("i=" + string(i) + ", creating list with ID " + string(ds) + ".");
        } else {
            ds = ds_map_create();
            show_debug_message("i=" + string(i) + ", creating map with ID " + string(ds) + ".");
        }
    }
}
Running it in GMS v1.1.750 results in the following:
Spoiler

This clearly indicates that the two data structure types maintain their handles separately.

GameGeisha

PS: I have just released an alternative for json_*() functions that can clean up even nested data structures with one script call, check it out and see if it can help make your life easier.

Edited by GameGeisha, 01 January 2013 - 03:51 AM.

  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users