On Thursday, 28 July 2016 at 15:02:58 UTC, Dechcaudron wrote:
On Thursday, 28 July 2016 at 14:43:32 UTC, Lodovico Giaretta
wrote:
No! Never run important finalization in a class destructor!
The GC is not obliged to run the destructors, so you may end
up with your objects destroyed but the connections still open.
For this kind of important things, you have to do them
manually.
I always thought that the moment of finalization is
undetermined, but that the GC does indeed run the destructor...
Weird, I'll have to look into that. After all what would be the
point of destructors if they are not guaranteed to be run?
The collector does not immediately finalize objects. It just
schedules them for finalization at a later time. So:
1) If you don't get low on memory, no collection is performed, so
no object is scheduled for finalization;
2) even if a collection is performed, false pointers may prevent
some unreachable object from becoming garbage and being scheduled
for finalization;
3) at program end, live objects are not scheduled for
finalization;
4) at program end, pending finalizations from previous
collections may not be run.