www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - virtual destructor in C++ integration: bug or me being stupid?

reply Atila Neves <atila.neves gmail.com> writes:
cpp.cpp:

class Oops {
public:

     virtual ~Oops() {}
     virtual int number() const { return 42; }
};

Oops* newOops() {
     return new Oops;
}

d.d:

import std.stdio;


extern(C++) {
     interface Oops {
         int number() const;
     }
     Oops newOops();
}

void main() {
     auto oops = newOops();
     writeln(oops.number());
}


I get garbage in the output (I found this due to a crash in much 
more complicated code). If I comment out the virtual destructor, 
it works.

It seems that the presence of the virtual destructor changes the 
layout and D doesn't know about it. Thinking about it now, it 
makes sense, how would D know?

The problem here is that I don't know what the workaround is. 
Abstract classes in C++ usually have virtual destructors...

Atila
Dec 29 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 29 December 2015 at 18:32:23 UTC, Atila Neves wrote:
 The problem here is that I don't know what the workaround is.
The one I used (well, last time I tried this) was to just put a dummy function in the D interface that is a placeholder for it. interface C++Class { // at the same place as void _dontCallMeIamjustadestructor(); void other_function_you_actually_want(); }
Dec 29 2015
parent Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 29 December 2015 at 18:41:41 UTC, Adam D. Ruppe wrote:
 On Tuesday, 29 December 2015 at 18:32:23 UTC, Atila Neves wrote:
 The problem here is that I don't know what the workaround is.
The one I used (well, last time I tried this) was to just put a dummy function in the D interface that is a placeholder for it. interface C++Class { // at the same place as void _dontCallMeIamjustadestructor(); void other_function_you_actually_want(); }
Ugh. As long as it works... thanks! Atila
Dec 30 2015