Destructor semantics

Michel Fortin michel.fortin at michelf.com
Tue Aug 10 17:10:44 PDT 2010


On 2010-08-10 19:58:08 -0400, Jonathan M Davis <jmdavisprog at gmail.com> said:

> On Tuesday, August 10, 2010 16:15:33 foobar wrote:
>> 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
> 
> If attempts to use any reference types in destructors were a compile-time error
> with a clear error message, that could go a long way in stopping people from
> trying to misuse destructors. As it is, there _are_ going to be plenty of D
> programmers who write destructors which access references to GC-allocated data
> and won't understand the weird bugs that they're getting.

Indeed.

But the compiler has no way to know at compile time whether a pointer, 
an array, or a reference points to GC-heap or somewhere else (global 
data or manually allocated). That's something only the programmer may 
know.

Perhaps this could be done for @safe mode however: disallow anything 
that could dereference a member (assuming it might be on the GC-heap).

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list