Releasing an object from its own method?

BlitzMax Forums/BlitzMax Beginners Area/Releasing an object from its own method?

Fry Crayola(Posted 2005) [#1]
Hi,

I'm trying to release the handle for an object from its own method (the method decreases its value, and when it reaches zero it kills it).

Is this possible? I tried using "Release Self" within an If statement, but that doesn't work, it tells me an expression must be a variable.

Any help is appreciated.


ImaginaryHuman(Posted 2005) [#2]
Isn't this what the Delete() method does if you define it?

I think you're meant to define a Delete() method inside the type, and this method will automatically be called when you release an instance of the type. I'm not sure if you can cause it to be released otherwise.


Fry Crayola(Posted 2005) [#3]
That could indeed work. The docs were a little hazy for me on exactly what I'm supposed to do.

Do I just need to define a method as follows:

Method Delete()
End Method

and let BlitzMax do the rest?


Dreamora(Posted 2005) [#4]
A release self is not needed.
Just remove it from the handling structure (normally the type list) and on the next flush mem it will be removed if it isn't referenced anymore

and no delete doesn't that job. Delete is called when the garbage collector is dereferencing the type instance


Perturbatio(Posted 2005) [#5]
I usually do types something like this:




Sweenie(Posted 2005) [#6]
Here is how I explained it to a friend.

Type Balloon

Method New()
Print "A balloon was created"
End Method

Method Delete()
Print "The balloon flown away"
End Method

End Type

Print ""
Print "Creating the balloon and get a reference handle to it"
Local BalloonString1:Balloon = New Balloon
Print ""

Print "Creating a second reference handle that connects to whatever BalloonString1 are connected to."
Local BalloonString2:Balloon = BalloonString1
Print ""

Print "Releasing the first handle"
BalloonString1 = Null ' "Cut" the first string
Print "Flush away all nonreferenced objects"
FlushMem()
Print ""

Print "Releasing the second handle"
BalloonString2 = Null ' "Cut" the second string
Print "Flush away all nonreferenced objects"
FlushMem()
Print ""