Beta D 2.069.0-b1

Rainer Schuetze via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Oct 14 06:11:59 PDT 2015



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?


More information about the Digitalmars-d-announce mailing list