Delete not getting called at all?
BlitzMax Forums/BlitzMax Programming/Delete not getting called at all?
| ||
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 |
| ||
Isn't Delete a reserved word. |
| ||
The method called Delete is meant to get called when the garbage collector gets around to freeing the type. |
| ||
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 |
| ||
Of course it doesn't get called in your above code. You never give it a chance to be called. =] |
| ||
Shouldn't it get called when the code quits? I imagine the GC still has to delete it. |
| ||
Or this example:Type Test Method Delete() DebugLog "Delete" EndMethod EndType blah() GCCollect End Function blah() Local a:Test=New Test End Function |
| ||
Hi, Max doesn't GCCollect at exit and relies on the OS to 'return' all memory used when the process exits. |
| ||
Oh ok, still seems abit inconsistent; I would've thought delete would get called atleast once on every instance. |
| ||
If you need it, OnEnd GCCollect? |
| ||
Doesn't that defeat the purpose of the automatic garbage collector? |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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 |
| ||
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. |
| ||
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. |
| ||
So whats the point of Delete if you can't be sure it will ever get called? |
| ||
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. |
| ||
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...). |
| ||
Such functions don't need to be contained in a Delete method, though. |
| ||
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.... |