www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11294] New: Object destruction with alias this

https://issues.dlang.org/show_bug.cgi?id=11294

          Issue ID: 11294
           Summary: Object destruction with alias this
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
          Assignee: stanislav.blinov gmail.com
          Reporter: zan77137 nifty.com
                CC: stanislav.blinov gmail.com
                CC: stanislav.blinov gmail.com
          Assignee: stanislav.blinov gmail.com

This code makes the behavior of the program strange:

----------------
/+
extern(C) void rt_finalize(void *ptr, bool det=true);
void clear(T)(T obj) if (is(T == class))
{
        rt_finalize(cast(void*)obj);
}
+/
import std.stdio;

class A {
        ~this(){writeln("A");}
        int dummy;
}
class B {
        A a;
        int dummy;
        alias a this;
        ~this(){writeln("B");}
}

void main() {
        auto a = new A;
        auto b = new B;
        b.a = a;
        writeln(b.dummy);
        clear(b);
        writeln(a.dummy);
        writeln("END");
}

RESULT:
----------------
$ dmd -run main
0
A
0
END
B

----------------
It is a problem to access the member of the destructed object.

So, the result should be like this:
----------------
$ dmd -run main
0
B
0
END
A

---------------
Because alias this is given priority to in the `cast(void*)obj`, this problem
occurs.

--
Nov 23 2018