C++ interop; object destruction

Manu via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 22 05:52:26 PDT 2017


How do I properly implement object destruction of extern(C++) classes (with
virtual destructors)?

I'm not going to embarrass myself by pasting some of my attempts to achieve
this goal to date.
I've asked before, but I've never yet seen a sufficiently solution to this
problem.

Situation:

I have a C++ base class expressed by an extern(C++) class.
C++ base class has virtual destructor.
I derive both C++ and D classes from this base class, they must each
destruct properly in their own right (object.destroy() crashes).
D passes base class pointers of D-derived classes to C++, and C++ calls the
virtual destructor.
D receives C++-derived classes by base class pointer, and needs to call the
virtual destructor (attempting to use object.destroy() crashes).

Forget about memory allocation, that's a separate (and solved) problem. The
only concern is proper destruction.

Can anyone who has had success in this space demonstrate how to structure
such code that will work?

Here is a C++ base class:

class Base
{
  virtual ~Base() { printf("destructing base"); }
};

I leave the rest of the puzzle as a clean slate. My attempts are all
disasters.

Note: I am happy to re-implement the C++ base class destructors on the D
size, as I expect that's necessary when derived D classes want to call
their base destructors.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20170622/f2c7ba27/attachment.html>


More information about the Digitalmars-d mailing list