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.
Comment #1 by github-bugzilla — 2018-11-27T01:50:12Z