GMS 2.3+ Can you delete a struct in one of its own functions?

I've tried something like this:
GML:
static free = function ()
{
    delete self;
}
However this always makes GM freak the hell out and fail to compile with the error Cannot set a constant ("@@This@@") to a value, even though I feel like it should work? Unless I'm being a moron and self doesn't work the way I think it does, or maybe I'm missing a built-in override for struct cleanup, similar to the __tostring method or whatever it's called.

The only way I've gotten this to work proper is to just pass itself in as an argument (though I haven't extensively tested if that even works proper).

GML:
static free = function (me)
{
    delete me;
}
this_struct.free(this_struct);
If there isn't an actual way to do this then I feel like that's a massive oversight.
 

DaveInDev

Member
if a struct is created as a variable in a scope, it is automatically deleted at the end the the scope.
And if you are creating it with "new", why don't you use "delete" to free it ? I do not see the need of this "free" function that you want. Do you want some kind of a destructor like in C++ ?
 

Juju

Member
delete struct; doesn't force a struct to be garbage collected. It's not like e.g. ds_map_destroy()

You should think of delete as deleting the reference to the struct but not the struct itself. Additionally, structs are garbage collected only when the last (strong) reference to the struct is lost. Through that lens, hopefully it's clear why what you're suggesting doesn't make sense as it would have no use.
 

DaveInDev

Member
delete struct; doesn't force a struct to be garbage collected. It's not like e.g. ds_map_destroy()

You should think of delete as deleting the reference to the struct but not the struct itself. Additionally, structs are garbage collected only when the last (strong) reference to the struct is lost. Through that lens, hopefully it's clear why what you're suggesting doesn't make sense as it would have no use.
Maybe I misunderstood the meaning of this page (delete is not strictly needed, but you can use it to "help" the GC):

"When a struct is no longer required it can be removed from memory using the delete operator, which is another new GML feature added in the 2.3 update. This de-references the struct and hints to the garbage collector that it may be available for collection. This is not strictly required as the garbage collector may do this automatically in the following game steps if the struct is no longer referenced in your code, but it is good practice to do so and we recommend it (for example, call delete in the Clean Up event of an instance to explicitly tell the garbage collector that an instance scope struct is to be deleted).
Also note that structs can be created using functions, which requires the use of the new operator and the keyword constructor (two more new features to GML), as shown in the following example:"

 
And if you are creating it with "new", why don't you use "delete" to free it ? I do not see the need of this "free" function that you want. Do you want some kind of a destructor like in C++ ?
Correct, I think.
For reference I'm using structs as an abstraction of my own file types, so they might have buffers or ds_maps I'd like to be able to clean up in addition to the struct itself once I'm done with it.

GML:
var hog = new HogFile();
hog.pack_name = "test";

hog.add_file("models/test.mdl", HogFlag.is_text, "{\n\tpretend\n\tthisisa:'json string'\n}");
hog.add_file("buffers/face.bff", 0, buffer_load(DSKT+"testface.bim"));

hog.save(DSKT+"hoggers.hog");
hog.free(); // loops through all the files in the "entries" map and frees up their data, then deletes the map, among other stuff.
delete hog;
Even still it feels kinda weird you can't use delete on itself in its own function.
 

FoxyOfJungle

Kazan Games
I think it can be considered "deleted" from memory, because if after I use delete struct and continue making a reference to this struct, it will cause errors.

In this case, there will be no more reference from the struct to the variable, as @Juju mentioned.
However, I can verify that it is still a struct, before accessing it, through the is_struct() function;
 

DaveInDev

Member
GML:
hog.free(); // loops through all the files in the "entries" map and frees up their data, then deletes the map, among other stuff.
delete hog;
That's the way I use it too :
a free() func that clear the internals, and then a call to delete (which is not always needed, as stated above, but recommanded by Yoyo anyway, to help the GC).
 
Top