www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: GC and "delete"

reply Brian White <bcwhite pobox.com> writes:
Would it be possible to add a lightweight flag to the GC module to 
detect if the GC was currently running?  That would allow an object to 
conditionally delete sub-objects only if it was safe to do so (i.e. only 
if not being deleted by the GC).

Alternatively, the "delete" could just do nothing at all during a GC 
pass.  This would probably be the safest solution.

-- Brian
Apr 05 2008
next sibling parent reply Gregor Richards <Richards codu.org> writes:
Brian White wrote:
 Would it be possible to add a lightweight flag to the GC module to 
 detect if the GC was currently running?  That would allow an object to 
 conditionally delete sub-objects only if it was safe to do so (i.e. only 
 if not being deleted by the GC).
 
 Alternatively, the "delete" could just do nothing at all during a GC 
 pass.  This would probably be the safest solution.
 
 -- Brian

The only time you would have user code running while the GC is running is in a destructor, and you shouldn't be accessing heap objects that potentially need to be deleted from the destructor anyway, since the order of destruction is unpredictable. - Gregor Richards
Apr 06 2008
parent reply Brian White <bcwhite pobox.com> writes:
 The only time you would have user code running while the GC is running 
 is in a destructor, and you shouldn't be accessing heap objects that 
 potentially need to be deleted from the destructor anyway, since the 
 order of destruction is unpredictable.

Which was exactly the problem I was trying to address. Sometimes (like with RAII), you want an object destructed when it goes out of scope and if that object contains other RAII objects, they must be deleted/destructed, too. However, I think I've got a better idea. Give me some time to write it up... -- Brian
Apr 06 2008
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
IMHO, the simple solution to this is an "opDelete" which gets called 
when and only when "delete x;" is used (afterward delete would be called 
as standard.)

-[Unknown]


Brian White wrote:
 The only time you would have user code running while the GC is running 
 is in a destructor, and you shouldn't be accessing heap objects that 
 potentially need to be deleted from the destructor anyway, since the 
 order of destruction is unpredictable.

Which was exactly the problem I was trying to address. Sometimes (like with RAII), you want an object destructed when it goes out of scope and if that object contains other RAII objects, they must be deleted/destructed, too. However, I think I've got a better idea. Give me some time to write it up... -- Brian

Apr 06 2008
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
== Quote from Brian White (bcwhite pobox.com)'s article
 Would it be possible to add a lightweight flag to the GC module to
 detect if the GC was currently running?  That would allow an object to
 conditionally delete sub-objects only if it was safe to do so (i.e. only
 if not being deleted by the GC).
 Alternatively, the "delete" could just do nothing at all during a GC
 pass.  This would probably be the safest solution.

This could be done in Tango via the collectHandler. Check out tango.core.Runtime. Sean
Apr 06 2008