On Wednesday, 14 October 2015 at 13:12:00 UTC, Rainer Schuetze wrote:

On 14.10.2015 14:57, Szymon Gatner wrote:
extern(C++)
void freeSubtract(Operation animal) {
   auto cat = cast(Subtract) animal; <<====== cast yields null
   if(cat !is null) {
     destroy(cat);
     free(cast(void*) cat);
   }
}

extern(C++)
void useOperation(Operation t) {
   auto res = t.execute(1, 2);
}

Everything works fine except the marked line. Subtract instance seems to be created correctly on D side as it returns valid result in the execute() call on C++ side but then when trying to free the instance on D side again, downcast from Operation to Subtract fails resulting in null.


To get compatible class layout, the D compiler has to omit it's class info entry in the vtable of C++ classes. In addition D doesn't know about C++ RTTI (I don't know if this is planned to add), so it cannot do the dynamic cast from Operation to Subtract.

I have not tried, but calling the virtual destructor instead of destroy() might just work?

Mint that this code comes from Adam Ruppe's book, and it is suppose to be working code. Also mind that Subtract is D class and instantiated on D side, it is only briefly passed to C++ for usage.

Reply via email to