import std.stdio;
import core.stdcpp.new_;
extern(C++) class A
{
int i;
~this(){}
}
class B : A
{
~this()
{
writeln("B.~this ", i);
}
}
void main()
{
A obj = cpp_new!B();
obj.i = 42;
cpp_delete(obj);
}
Compiling and running the above code using ldc2 -L-lstdc++ -m32 -run test.d prints a wrong result like B.~this -401281917. The same happens when using obj.__dtor(), obj.__xdtor() or obj.destroy().
It only happens, when the base class is extern(C++) and the derived class is extern(D), so a workaround is to also declare class B as extern(C++).
Another workaround is cast the destructor to the used ABI, like (cast(void delegate())&obj.__xdtor)(), but that only works when the used ABI is known.
DMD prints the correct value, so it seems to be a bug in the LDC backend.
Compiling and running the above code using
ldc2 -L-lstdc++ -m32 -run test.dprints a wrong result likeB.~this -401281917. The same happens when usingobj.__dtor(),obj.__xdtor()orobj.destroy().It only happens, when the base class is
extern(C++)and the derived class isextern(D), so a workaround is to also declareclass Basextern(C++).Another workaround is cast the destructor to the used ABI, like
(cast(void delegate())&obj.__xdtor)(), but that only works when the used ABI is known.DMD prints the correct value, so it seems to be a bug in the LDC backend.