www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19119] New: App crashes with strange behavior of ctors/dtors

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

          Issue ID: 19119
           Summary: App crashes with strange behavior of ctors/dtors from
                    CPP
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: vladimmi gmail.com

After update 2.081 I'm experimenting with binding external CPP classes with
extern(C++). I use simple class to test:

---testlib.cpp
#include <iostream>

class Bar {
public:
    Bar() {}
    ~Bar() {}
};

class __declspec(dllexport) Foo {
    int *a;
    Bar *b;

public:
    Foo(int value) {
        std::cout << "Creating in CPP\r\n";
        a = new int(value);
        b = new Bar();
        std::cout << "Done\r\n";
    }

    ~Foo() {
        std::cout << "Deleting in CPP\r\n";
        delete a;
        delete b;
        std::cout << "Done\r\n";
    }
};
---

This code is compiled with "cl testlib.cpp", "lib /out:testlib.lib testlib.obj"
and resulting lib linked to D app.

D side:

---app.d
import std.stdio;

extern(C++) {
    class Foo {
        this(int value);
        final ~this();
    }
}

void main() {
    for (int i; i < 100; i++) {
        writeln("Creating in D");
        Foo bar = new Foo(0);
        writeln("Deleting in D");
        bar.destroy();
    }
    writeln("Finished");
    readln();
}
---

This example works perfect. But when I change Foo::~Foo to virtual (and remove
"final" on D side) app silently crashes on exactly 3rd iteration after
"Creating in D" step. So, for some reason, I've changed destructor but it
crashes on/before constructor. And, for some reason, I have no error messages
or something - application just hangs a bit and stops.

This seems to work OK if Foo contains only int*, no other objects.

Same results on both DMD 2.081.1 and LDC 1.11.0-beta2.

--
Jul 26 2018