Releasing an object from its own method?

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

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.

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.

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?

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

I usually do types something like this:

Type TMyType
	Global List:TList
	Field value:Int
	
	Function Free(t:TMyType)
		If ListContains(List, t) Then ListRemove(List, t)
		t = Null
		FlushMem
	End Function
	
	Function Create:TMyType(val:Int)
		Local tempMyType:TMyType = New TMyType
			tempMyType.value = val
			If Not List Then List = New TList
			List.AddLast(tempMyType)
		Return tempMyType
	End Function
End Type


TMyType.Create(10)
TMyType.Create(14)
TMyType.Create(17)

For Local t:TMyType = EachIn TMyType.List
	Print t.Value 'print the values
	TMyType.Free(t) 'free the type
Next


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 ""