Object Delete

BlitzMax Forums/BlitzMax Beginners Area/Object Delete

H&K(Posted 2006) [#1]
When do I need to create a destroy/Free Method for Types

I realize the Good OOP answer is every time I have a Type definition

But Come on really, when do I need to explicitly destroy fields of the object


Dreamora(Posted 2006) [#2]
When they reference something cyclic, then for sure. otherwise you will have phantom objects in ram that won't be freed but that can't be deleted.

So -> normally always as TList is a cyclic structure for example


H&K(Posted 2006) [#3]
So in the case of a field Tlist, the field is a pointer to the list, so when the object is destroyed, only the pointer is detroyed?

What about an array?

On A related but differnet Topic.

What function is called when an object is internaly deleted. (i.e. What function of "TObject" do I need to override to automaticaly delete TList and stuff?


Dreamora(Posted 2006) [#4]
Method Delete is called or should be called we should better say. But TList is the best proof that is not always called and if clear is not called, the list will remain in RAM.


No, not only the pointer is destroyed but the TList object as well ... sadly, this does not mean that all TLinks are freed which is not the case if method delete does not get called which seems to happen ...


H&K(Posted 2006) [#5]
So it would be as complicated as I thought, if it worked properly :)


FlameDuck(Posted 2006) [#6]
The smart-ass answer is "If you don't know, you don't need it".

The slightly more useful answer is "Whenever you're doing something that isn't managed. Like MemAlloc, or interfacing with other langauges". Also in the old days (not sure whether this has been addressed or not) Streams where notorisly bad at remembering to 'close' themselves, and would often leave 'locked' files on the filesystem or open TCP connections.

What function is called when an object is internaly deleted.
The Delete() method.

Cyclical references is really a bad example, as objects in a cyclic datastructure, never get garbage collected, and thus their Delete() method is never invoked, unless you do so explicitly, in which case it would probably be better to call it something other than Delete(), as changes are it'll crash when invoked a second time by the garbage collector.


H&K(Posted 2006) [#7]
@ "If you dont know, you dont need it"

Surly that could be an answer to every question in the forums?

Windows Hooks.
If I realease an object that has invoked a windows hook, do I need to release the Hook explicitly


Dreamora(Posted 2006) [#8]
Yepp
And in some cases its really the answer ... there are topics that are advanced to pro level and it makes about no sense trying to explain them to beginners that do not even understand OO so far for example ... :-)


Grey Alien(Posted 2006) [#9]
Basically I pretty much pretend there is no garbage collection and clear everything old-style and if I miss anything, I hope that the GC will collect it. I have the allocated memory showing on some debug stats and keep and eye on that too.


Chris C(Posted 2006) [#10]
from time to time its a good idea to keep an eye on memory allocations from the windows side of things with a decent process explorer like the one from sysinternals(.com?)