Destructor or Delete in BMax

BlitzMax Forums/BlitzMax Programming/Destructor or Delete in BMax

Midnight(Posted 2006) [#1]
I am confused about the use of the destructor in BMax.

I see reference to the "Delete" method, but no documentation on how to use it. Looking at sample programs, I can see that people are using Remove:

method DestroyMe()
mylist.Remove(Self)
end method

That obviously removes the reference to the object I created, but it doesn't actually remove the object itself, does it? Isn't this object still occupying memory until it is destroyed (deleted)?

So how do I use the destructor in BMax?


Dreamora(Posted 2006) [#2]
the object is still there, yes.

But if this was the last reference to this specific object, the GarbageCollector will free the object and call the Delete() method (so you can put all other clean up code in there)

Memory occupation is something that is not directly bound to your current objects, as it keeps some memory within a memory pool to assign new objects to it without the need to ask the OS all the time to get / free memory as this isn't the fastest thing.


H&K(Posted 2006) [#3]
Unless it stops working properly (ie self linking lists and the like), the GC will destroy the object once the last intsance of it point to Null

Use delete, untill it looks like it isnt working


Warren(Posted 2006) [#4]
One thing to remember is that you can't rely on Delete() being called on your object so don't do anything critical in there. If the garbage collector never collects that object, Delete() will never be called. If you have lots of memory and the GC never needs to collect old objects - ouch.


Dreamora(Posted 2006) [#5]
And at which point breaks that the "rely on delete beeing called"?

Not without ref -> not collected -> naturally delete never called.

You will most likely always need another method to clean the "back references" first (normall remove it from a list or similar structure where it is saved for handling)


Midnight(Posted 2006) [#6]
Hmm, this still isn't quite clear to me. So I call the following method in my object:

method DestroyMe()
mylist.Remove(Self)
end method

This, since it removes the only reference to the object (I am assuming that it is not in any other lists), will initiate the Garbage Collection to call the delete method of my object (when it get's around to it). So I need the following code in my object:

method Delete()
Some code here...
End Method

Is that correct so far? If so, then what code do I need to place in the Delete() method that would destroy the object?

Or do I not need any, since the GC will automatically destroy the object anyway?

Thanks!


Gabriel(Posted 2006) [#7]
Nothing is necessary there in general. It's there for any code you want to be executed when the object is destroyed. By the time the Delete() method is called, the object is already on it's way out, so you don't *have* to do anything.


Hotcakes(Posted 2006) [#8]
To 'delete' your objects... simply do stuff like this:

object:ObjectType = New ObjectType
'no longer needed:
object = Null


Since your object is dereferenced (ie your code can no longer access it), the garbage collector will notice this and -at it's own discretion-, free up or reuse the space taken by that object in its own time. If there was a Delete method attached to ObjectType, the code in that method will be executed when the GC does its thing - which may or may not ever happen. I'm a bit rusty as I havn't used Max in some time, but I think your DestroyMe method would look more like:

Method DestroyMe
mylist(Self)=Null
'or
mylist.Self=Null
End Method


I forget.


TomToad(Posted 2006) [#9]
Check this tutorial:
http://www.blitzbasic.com/Community/posts.php?topic=59233


Midnight(Posted 2006) [#10]
Thanks everyone! I think I got it now...

Toby: i've seen both:
mylist.self=null
and
mylist.Remove(Self)
used in examples, so I assume either can be used.

Tom: Yup, I had read that. Great doc, but unfortunately weak on the explanation of the destructor. Basically just say there is one, and it's just like using NEW. No examples.

Again, thanks everyone!