Destructor

BlitzMax Forums/BlitzMax Programming/Destructor

jkrankie(Posted 2009) [#1]
I suspect the answer is no, but is there any way to access a destructor?

Cheers
Charlie


Htbaa(Posted 2009) [#2]
Not sure what you mean really. Do you mean calling the destructor of an object? It only gets called when an object isn't being referenced anymore and the Garbage Collector collects it.

I think you can force it, however. Just set your object to Null and then call the GCCollect(). But be sure no other object's or variables refer to it.


Grisu(Posted 2009) [#3]
You can always use: GCSetMode() and do the cleaning by hand I guess.


Brucey(Posted 2009) [#4]
The Delete() method is called when your object is GC'd.

You can add your own implementation of Delete() if you want to tidy up some of your own stuff.
eg.
Type mytype

    Method Delete()
        ' called when I am GC'd
    End Method

End Type


Note though, that on Application exit, these are unlikely to be called - so you wouldn't put any "really important save state to disk" code in there, or the likes.

In my modules I use Delete() for freeing memory allocated by non BlitzMax code.

And remember, it is only called when the particular object is ready for garbage collection - ie. no other references to it exist.


jkrankie(Posted 2009) [#5]
Ok, cool. It's for MiniB3d, which insists i manually free every entity i create using freeentity(). this wouldn't be a pain, but because the vast bulk of my entities are contained in classes, it ends up adding to code, as i have been adding a freeAll() method to each of my classes.

i also don't need it on application exit, thanks to the ever handy clearworld() function :) So hopefully overriding the default Delete method will work while the program is in full swing.

Cheers
Charlie


Brucey(Posted 2009) [#6]
Well, if the entity is referenced by a list somewhere, it would never turn up for GC'ing, which I suppose is why you need to call FreeEntity() yourself.

Of course, if you have an object which contains the entity, then you can Free the entity in your Delete() method.


jkrankie(Posted 2009) [#7]
If i use the Delete() method, can just have the freeentity bits in there and the GC will act as normal in addition to freeing the entity, or would i need to add anything else?

Cheers
Charlie