[Issue 11294] New: Object destruction with alias this
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Nov 23 09:38:43 UTC 2018
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 at gmail.com
Reporter: zan77137 at nifty.com
CC: stanislav.blinov at gmail.com
CC: stanislav.blinov at gmail.com
Assignee: stanislav.blinov at 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.
--
More information about the Digitalmars-d-bugs
mailing list