ds_list still gets value pointed to after deleting[SOLVED]

When I execute this code:
Code:
dsListOld = ds_list_create();
ds_list_add(dsListOld,"old");
ds_list_destroy(dsListOld);
dsListNew = ds_list_create();
ds_list_add(dsListNew,"new");
and look in the debugger then the debugger shows me that there is a dsListOld which contains the value "new" and there is a dsListNew which contains the value "new".
Why?
I know that the first ds_list gets the value 0 but doesnt it get deleted after the ds_list_destroy event?
 
H

Homunculus

Guest
First of all, I don't see dsListOld nor dsListNew declared anywhere in your code. I assume you wanted to use those names instead of dslist and dslist1.

When you create a data structure, you get a numerical index in return. Ideally, if dsListOld is the first list you create in the game, your dsListOld variable would hold the number 0. Not a pointer, not a reference, just plain 0. This numerical index is not unset when you destroy the list, meaning that it doesn't matter what happens to the list itself, your dsListOld variable will still hold 0 (or another numerical index), which may or may not point to an existing ds_list.

When you destroy dsListOld though, the index 0 will be available again, and therefore will be assigned to the next list you create, dsListNew, which now can be referenced, due to what I described above, also by the variable dsListOld holds.

Ideally by the end of that code you could access the "new" string by using any of those lines:
Code:
ds_list_find_value(dsListNew, 0);
ds_list_find_value(dsListOld, 0);
ds_list_find_value(0, 0);
The above are all valid (not necessarily optimal) ways to access your value, assuming your code is run before any other list is created (otherwise the third line should be something like ds_list_find_value(1, 0); or ds_list_find_value(873, 0);).
 
First of all, I don't see dsListOld nor dsListNew declared anywhere in your code. I assume you wanted to use those names instead of dslist and dslist1.
That is weird though because after posting this I immediately edited the post.

When you create a data structure, you get a numerical index in return. Ideally, if dsListOld is the first list you create in the game, your dsListOld variable would hold the number 0. Not a pointer, not a reference, just plain 0. This numerical index is not unset when you destroy the list, meaning that it doesn't matter what happens to the list itself, your dsListOld variable will still hold 0 (or another numerical index), which may or may not point to an existing ds_list.

When you destroy dsListOld though, the index 0 will be available again, and therefore will be assigned to the next list you create, dsListNew, which now can be referenced, due to what I described above, also by the variable dsListOld holds.
Wow, so the ds_exists(id,type) function is total useless?
example:
Code:
dsListOld = ds_list_create();
ds_list_add(dsListOld,"old");
ds_list_destroy(dsListOld);
dsListNew = ds_list_create();
ds_list_add(dsListNew,"new");
if(ds_exists(dsListOld,ds_type_list))
{
    show_debug_message("why does this keep happening");
    show_debug_message(ds_list_find_value(dsListOld,0));
}
So, how do I handle dslist so that my data doesnt get mixed up?
EDIT: sorry happened again renamed dslist1 to dsListNew
 
H

Homunculus

Guest
Yeah, it's kind of useless. You can easily prevent this though by unsetting the variable yourself after destroying the list:
Code:
dsListOld = ds_list_create();
ds_list_add(dsListOld,"old");
ds_list_destroy(dsListOld);
dsListOld = -1; //unset
dsListNew = ds_list_create();
ds_list_add(dsListNew,"new");
 
Yeah, it's kind of useless. You can easily prevent this though by unsetting the variable yourself after destroying the list:
Code:
dsListOld = ds_list_create();
ds_list_add(dsListOld,"old");
ds_list_destroy(dsListOld);
dsListOld = -1; //unset
dsListNew = ds_list_create();
ds_list_add(dsListNew,"new");
Thank you very much! This really helps alot! I think they should make it automatically setting it to -1! Why is this not automatically done?
Ok I will set this thread to solved.
 

Mike

nobody important
GMC Elder
All that happens in a ds_list_create(), is that it allocates an internal "slot". So if you create, delete then create again - you'll reuse that slot.
You're getting back a handle, you should never assume anything about how the internals work and always reset the variable yourself.

It also can't set the variable as the variable "handle" isn't passed in, just it's value, so it's up to you to reset.

As said above, setting to -1 on deletion should fix your issues...
 
H

Homunculus

Guest
As long as GM uses reals as data structure ids they can’t really unset them for you. Those should become pointers (like in the case of arrays) for that to happen, but this change would invalidate a lot of existing code I guess, can’t be done lightly
 
Top