Why not memory specific destructors?

Frustrated via Digitalmars-d digitalmars-d at puremagic.com
Mon May 5 11:08:19 PDT 2014


I never got the real issue with destructors(I haven't seen the 
issue explained, just a lot of talk about it being a problem and 
how to fix it) but I think doing away with them would be a very 
bad idea. Assuming the only/main issue is with the GC not 
guaranteeing to call them then that is really throwing out the 
baby with the bathwater.

Some of us do not want to be locked down by the GC. If you shape 
the D language around using the GC then you just our hole deeper 
and deeper. (We are trying to get out of this hole, remember?)


So, instead of removing destructors why not have multiple types? 
If the object is manually allocated then we can guarantee the 
destructor will be called when the object is free'ed.

But basically, since they would be different types of destructors 
there would be no confusion about when they would or wouldn't be 
called.

1. GC destructors - Never called when the object is managed by 
the GC. (or maybe one can flag certain ones to always be called 
and the GC will respect that)

2. Manual memory management destructors - Always called when the 
object is allocated by manually.

3. Others(ARC, etc) - Same principle.


So, while this could provide different behavior depending on how 
you use memory(not a great thing but possibly necessary), it at 
least provides the separation for a choice. (and it's a about 
choice, not about forcing people to use something that doesn't 
work for them)




It seems to me we have 4 basic lifetimes of an object:

1. Fixed/Physical Scope - The object lives and dies very quickly 
and is well defined.

2. UnFixed/Logical Scope - The scope is not well defined but 
something somewhere free's the object in a predictable way when 
it(the programmer) decides it should be free'ed).

3.  Auto Scope - A combination of the above where an object can 
live in both at the same time and automatically determines when 
it goes out of the the last scope. This is like ARC type of stuff.

4. Unknown/Non-Deterministic/Unpredictable -  There are no 
scopes. Objects lifetimes are completely handled by God(the GC). 
We don't have to worry about any of it. Unfortunately D's GC 
hasn't had it's `god mode` flag set.


1 and 2 essentially are old school manual memory management.


If we have object's lifetimes that exist in different ways then 
having different destructors for these possibilities seems 
logical. The problem may simply be that we are trying to fit one 
destructor to all the cases and it simply doesn't work that way.

Anyways... just food for thought.


More information about the Digitalmars-d mailing list