www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ interop; object destruction

reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
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.
Jun 22
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:
 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 take it you tried calling super.~this() already? that would be my first thought.
Jun 22
parent ketmar <ketmar ketmar.no-ip.org> writes:
Nicholas Wilson wrote:

 On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:
 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 take it you tried calling super.~this() already? that would be my first thought.
that won't work, 'cause `~this` is not a valid symbol. yet `super.__dtor()` will work.
Jun 22
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:
 How do I properly implement object destruction of extern(C++) 
 classes (with
 virtual destructors)?

 [...]
Also didn't Ethan come up with a solution for this for Binderoo?
Jun 22
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 23 June 2017 at 09:32, Nicholas Wilson via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:

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

 [...]
Also didn't Ethan come up with a solution for this for Binderoo?
I'm not sure...
Jun 22
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 23 June 2017 at 00:33:55 UTC, Manu wrote:
 On 23 June 2017 at 09:32, Nicholas Wilson via Digitalmars-d < 
 digitalmars-d puremagic.com> wrote:

 On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:

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

 [...]
Also didn't Ethan come up with a solution for this for Binderoo?
I'm not sure...
I'm pretty sure he manually constructs the vtbl and inserts the virtual function into it, so it should just be a case of calling the virtual destructor. Have a look at this DConf 2017 talk.
Jun 22
next sibling parent Danni Coy via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Fri, Jun 23, 2017 at 10:52 AM, Nicholas Wilson via Digitalmars-d <
digitalmars-d puremagic.com> wrote:


 I'm pretty sure he manually constructs the vtbl and inserts the virtual
 function into it, so it should just be a case of calling the virtual
 destructor.

 Have a look at this DConf 2017 talk.
He is mapping C++ classes onto D structs IIRC.
Jun 22
prev sibling next sibling parent Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 23 June 2017 at 10:52, Nicholas Wilson via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 On Friday, 23 June 2017 at 00:33:55 UTC, Manu wrote:

 On 23 June 2017 at 09:32, Nicholas Wilson via Digitalmars-d <
 digitalmars-d puremagic.com> wrote:

 On Thursday, 22 June 2017 at 12:52:26 UTC, Manu wrote:
 How do I properly implement object destruction of extern(C++) classes
 (with
 virtual destructors)?

 [...]
Also didn't Ethan come up with a solution for this for Binderoo?
I'm not sure...
I'm pretty sure he manually constructs the vtbl and inserts the virtual function into it, so it should just be a case of calling the virtual destructor. Have a look at this DConf 2017 talk.
I do that, but that doesn't mean the destructors work. object.destroy() just crashes. Believe me, I've been messing with this a lot. The answer to this email is some code that satisfies the criteria, I've tried a lot of things.
Jun 22
prev sibling parent Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 23 June 2017 at 13:45, Danni Coy via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 On Fri, Jun 23, 2017 at 10:52 AM, Nicholas Wilson via Digitalmars-d <
 digitalmars-d puremagic.com> wrote:


 I'm pretty sure he manually constructs the vtbl and inserts the virtual
 function into it, so it should just be a case of calling the virtual
 destructor.

 Have a look at this DConf 2017 talk.
He is mapping C++ classes onto D structs IIRC.
Oh yeah that's right, that's a totally different ball-game :)
Jun 22