Summary: Destructors are inherently u...@safe
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Michel Fortin <> 2010-08-11 
14:08:33 EDT ---
Accessing the GC heap through a member in a destructors is inherently unsafe
because the GC might have already freed that memory. So destructors in SafeD
should not be able to access the GC-heap through a member. Here is an example:


class C {
    C other;
    ~this() {
        writeln(other.toString()); // "other" might already have been freed.

void main() {
    C c1 = new C;
    C c2 = new C;
    c1.other = c2;
    c2.other = c1; // creating a circular reference

Given that the compiler has no way to know if a reference, pointer, or array
points to the GC heap or elsewhere, it might have to disallow any dereferencing
of any member and calls to functions that might dereference a member. And at
this point you can't do anything useful in a destructor, so you might just
disallow @safe destructors altogether.

Note that this applies to struct destructors too, since structs can be on the
heap (in their own memory block, part of an array, or as a member of a class).

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to