Skip to content

extern(C++) destructor uses wrong ABI on x86 in derived class #5260

Description

@tim-dlang
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions