Thread overview
C++ interop; object destruction
Jun 22, 2017
Manu
Jun 22, 2017
Nicholas Wilson
Jun 22, 2017
ketmar
Jun 22, 2017
Nicholas Wilson
Jun 23, 2017
Manu
Jun 23, 2017
Nicholas Wilson
Jun 23, 2017
Danni Coy
Jun 23, 2017
Manu
Jun 23, 2017
Manu
June 22, 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.


June 22, 2017
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.
June 22, 2017
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.
June 22, 2017
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?
June 23, 2017
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...


June 23, 2017
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.
June 23, 2017
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.


June 23, 2017
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.


June 23, 2017
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 :)