Destructor semantics

foobar foo at bar.com
Tue Aug 10 16:15:33 PDT 2010


Michel Fortin Wrote:

> On 2010-08-10 18:39:03 -0400, "Steven Schveighoffer" 
> <schveiguy at yahoo.com> said:
> 
> > It's only safe if the struct destructor does not deallocate any heap data.
> 
> To be even clearer, it's only safe if the struct destructor doesn't 
> access any heap data through one of its members.
> 
> And accessing an array or a string could potentially count as accessing 
> heap data too (unless you can be sure it wasn't from the GC-heap).
> 
> So destructors called during the collection cycle are a very tricky 
> thing to handle.
> 
> -- 
> Michel Fortin
> michel.fortin at michelf.com
> http://michelf.com/
> 

I agree :)

In general, the rule is that destructors can only access value types. This can be enforced at compile time. 
Also, I was using structs and classes in my examples but this should be understood as value types vs. reference types. 

For example, when a dtor for a fixed-sized array is called (value type) it will in turn call dtors for its elements



More information about the Digitalmars-d mailing list