<div dir="ltr"><div>How do I properly implement object destruction of extern(C++) classes (with virtual destructors)?</div><div><br></div><div>I'm not going to embarrass myself by pasting some of my attempts to achieve this goal to date.</div><div>I've asked before, but I've never yet seen a sufficiently solution to this problem.</div><div><br></div><div>Situation:</div><div><br></div><div>I have a C++ base class expressed by an extern(C++) class.</div><div>C++ base class has virtual destructor.</div><div>I derive both C++ and D classes from this base class, they must each destruct properly in their own right (object.destroy() crashes).</div><div>D passes base class pointers of D-derived classes to C++, and C++ calls the virtual destructor.</div><div>D receives C++-derived classes by base class pointer, and needs to call the virtual destructor (attempting to use object.destroy() crashes).</div><div><br></div><div>Forget about memory allocation, that's a separate (and solved) problem. The only concern is proper destruction.</div><div><br></div><div>Can anyone who has had success in this space demonstrate how to structure such code that will work?</div><div><br></div><div>Here is a C++ base class:</div><div><br></div><div>class Base</div><div>{</div><div>  virtual ~Base() { printf("destructing base"); }</div><div>};</div><div><br></div><div>I leave the rest of the puzzle as a clean slate. My attempts are all disasters.</div><div><br></div><div>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.</div></div>