Delete not getting called at all?

BlitzMax Forums/BlitzMax Programming/Delete not getting called at all?

Azathoth(Posted 2005) [#1]
Running the example, the overloaded method Delete doesn't seem to be getting called.

Type Test
	Method Delete()
		DebugLog "Delete"
	EndMethod
EndType

Local a:Test=New Test



tonyg(Posted 2005) [#2]
Isn't Delete a reserved word.


Azathoth(Posted 2005) [#3]
The method called Delete is meant to get called when the garbage collector gets around to freeing the type.


tonyg(Posted 2005) [#4]
Ok, but does your object ever go out of scope to be deleted?
Type Test
	Method Delete()
		DebugLog "Delete"
	EndMethod
EndType

Local a:test=New Test
a=Null
GCCollect



Hotcakes(Posted 2005) [#5]
Of course it doesn't get called in your above code. You never give it a chance to be called. =]


Azathoth(Posted 2005) [#6]
Shouldn't it get called when the code quits? I imagine the GC still has to delete it.


Beaker(Posted 2005) [#7]
Or this example:
Type Test
	Method Delete()
		DebugLog "Delete"
	EndMethod
EndType


blah()
GCCollect
End

Function blah()
	Local a:Test=New Test
End Function



marksibly(Posted 2005) [#8]
Hi,

Max doesn't GCCollect at exit and relies on the OS to 'return' all memory used when the process exits.


Azathoth(Posted 2005) [#9]
Oh ok, still seems abit inconsistent; I would've thought delete would get called atleast once on every instance.


taxlerendiosk(Posted 2005) [#10]
If you need it, OnEnd GCCollect?


Azathoth(Posted 2005) [#11]
Doesn't that defeat the purpose of the automatic garbage collector?


Koriolis(Posted 2005) [#12]
Huh? No.
Just put "OnEnd GCCollect" at the start of your program and forget about it. You'll then have an automatic collection at exit.


taxlerendiosk(Posted 2005) [#13]
Azthoth: Look up "OnEnd" in the docs, it adds a function (no parameters allowed) to a list of functions that are called when the program is finally exited.


Azathoth(Posted 2005) [#14]
I know what OnEnd and GCCollect does. The point is its meant to already be automatic, you shouldn't need to make it anymore automatic. GCCollect is mainly used if you've set the GC to manual, by default its automatic.


Ferminho(Posted 2005) [#15]
It's meant to be automatic during runtime, not at app exit.

I find it ok the way it works now, as GCCollect doesn't free memory but deallocate it for posterior use - if program is about to end, *normal* behavious is let the OS free the memory, not deallocate everything and then free it. It's like fixing something you're about to trash; wasted time.
Well, that's my $0.02


Fabian.(Posted 2005) [#16]
Sorry, but sometimes Delete() not only frees memory.
So Delete() should be called by program's end.
I think there should be an "OnEnd GCCollect" call in brl.appstub or brl.blitz.


marksibly(Posted 2005) [#17]
Hi,

You shouldn't rely on Delete to release 'critical' resources as you can't be sure when it will be called. This is why you need to CloseStream - relying on Delete() could result in too many files being open at once (it can happen!).

Also, 'OnEnd GCCollect' will have little effect as it wont release globals. To properly do this, Max would have to generate code to 'Null out' all globals and then GCCollect. Modern OS's do a fine job of cleaning up memory/files/dlls/resources when an app exits, so doing this is pretty redundant anyway.


Azathoth(Posted 2005) [#18]
So whats the point of Delete if you can't be sure it will ever get called?


Hotcakes(Posted 2005) [#19]
To clean up your program's environment, not to run important tasks.

I think there should be an "OnEnd GCCollect" call in brl.appstub or brl.blitz.

GCCollect, just like FlushMem, is not a guarantee that your memory will be deleted anyway. It doesn't tell the GC to collect redundant memory, it tells the GC to consider collecting redundant memory.


Fabian.(Posted 2005) [#20]
Toby Zuydidlyfink:
To clean up your program's environment, not to run important tasks.
Often it's needed to run important tasks to clean up the program's enviroment (For example saving system configurations...).


Hotcakes(Posted 2005) [#21]
Such functions don't need to be contained in a Delete method, though.


Fabian.(Posted 2005) [#22]
Azathoth:
still seems abit inconsistent;
That's true, I think having no Delete at program's End is like not closing all html elements before end of html file:
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    anything....