Skip to content

Field destructor not called for extern(C++) class without explicit destructor #22942

Closed

Description

@tim-dlang

Found during review of PR #22931.

import std.stdio;

extern(C++):
struct S
{
    ~this()
    {
        writeln("S.~this");
    }
}

class A
{
    ~this()
    {
        writeln("A.~this");
    }
}

class B : A
{
    S s;
}

void main()
{
    A x = new B;
    x.destroy();
}

It only runs the destructor for A and does not run the destructor for S, because B has no explicit destructor. This probably happens, because cppDtorVtblIndex not set (-1) for class B. See

if (cldec.classKind == ClassKind.cpp && cldec.cppDtorVtblIndex != -1)
{
// now we've built the aggregate destructor, we'll make it virtual and assign it to the reserved vtable slot
cldec.dtor.vtblIndex = cldec.cppDtorVtblIndex;
cldec.vtbl[cldec.cppDtorVtblIndex] = cldec.dtor;
if (target.cpp.twoDtorInVtable)
{
// TODO: create a C++ compatible deleting destructor (call out to `operator delete`)
// for the moment, we'll call the non-deleting destructor and leak
cldec.vtbl[cldec.cppDtorVtblIndex + 1] = cldec.dtor;
}
}

Only class A needs to be extern(C++) for this to happen. Subclass B can be extern(C++) or extern(D).

Activity

  1. added a commit that references this issue on Jul 25, 2026
    4d26e03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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