[Issue 21693] New: extern(C++) class instance dtors are never called, breaking RAII

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 8 23:42:27 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21693

          Issue ID: 21693
           Summary: extern(C++) class instance dtors are never called,
                    breaking RAII
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: thomas.bockman at gmail.com

The program below never destroys its extern(C++) class instances, breaking
RAII. Output since 2.066:

    Destroying stack D instance.
    Destroying heap D instance.

The destructor should definitely be called for the `scope` instance, as it
would be for a struct on the stack. The heap instance's destructor should also
be called, OR trying to allocate an instance of an extern(C++) class that has a
destructor on the GC heap using `new` should be a compile-time error.

/////////////////////////////////////
extern(C++) class C {
    bool onStack;
    this(bool onStack) {
        this.onStack = onStack; }
    ~this() {
        writeln("Destroying ", onStack? "stack" : "heap", " C instance."); }
}
extern(D) class D {
    bool onStack;
    this(bool onStack) {
        this.onStack = onStack; }
    ~this() {
        writeln("Destroying ", onStack? "stack" : "heap", " D instance."); }
}

void main() {
    scope stackC = new C(true);
    auto heapC = new C(false);
    scope stackD = new D(true);
    auto heapD = new D(false);
}
/////////////////////////////////////

Related: https://issues.dlang.org/show_bug.cgi?id=21692

--


More information about the Digitalmars-d-bugs mailing list