Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual destructors and deleting objects with multiple inheritance... How does it work?

Tags:

c++

First, I understand why virtual destructors are needed in terms of single inheritance and deleting an object through a base pointer. This is specifically about multiple inheritance and the reason behind why this works. This question came up in one of my university classes, and no one (including the professor) was sure why this worked:

#include <iostream>  struct A {     virtual ~A()     {         std::cout << "~A" << std::endl;     }     int memberA; };  struct B {     virtual ~B()     {         std::cout << "~B" << std::endl;     }     int memberB; };  struct AB : public A, public B {     virtual ~AB()     {         std::cout << "~AB" << std::endl;     } };  int main() {     AB* ab1 = new AB();     AB* ab2 = new AB();      A* a = ab1;     B* b = ab2;      delete a;     delete b; } 

The output for this is:

~AB
~B
~A
~AB
~B
~A

How does the compiler know how to call A's and B's destructor when deleting a or b? Specifically, how is the memory for AB laid out (particularly it's virtual function table), such that the A and B destructors can be called?

My professor was suggesting that memory would be laid out (something) like this:

    AB +---------+              +----+ |  A VFT  | - - - - - -> | ~A | +---------+              +----+ | memberA | +---------+              +----+ |  B VFT  | - - - - - -> | ~B | +---------+              +----+ | memberB | +---------+  // I have no idea where ~AB would go... 

We're all curious how these destructors are actually laid out in memory and how calling delete on either a or b results in all the destructors being properly called. It makes sense that deleting a base object works in single inheritance (because there's a single virtual function table to work with), but apparently I'm not understanding things correctly because I can't take my understanding of the single inheritance version and apply it to this multiple inheritance example.

So how does this work?

like image 847
Cornstalks Avatar asked Apr 04 '13 17:04

Cornstalks


People also ask

How does destructor work in inheritance?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

Are virtual destructors inherited?

Yes, they are the same. The derived class not declaring something virtual does not stop it from being virtual. There is, in fact, no way to stop any method (destructor included) from being virtual in a derived class if it was virtual in a base class.

What is the purpose of virtual destructor in C++?

A virtual destructor is used to free up the memory space allocated by the derived class object or instance while deleting instances of the derived class using a base class pointer object.

What happens if virtual Desctructor is not used in C++?

If your base class destructor is NOT virtual then only base class object will get deleted(because pointer is of base class "Base *myObj"). So there will be memory leak for derived object.


1 Answers

It works because the standard says that it works.

In practice, the compiler inserts implicit calls to ~A() and ~B() into ~AB(). The mechanism is exactly the same as with single inheritance, except that there are multiple base destructors for the compiler to call.

I think the main source of confusion in your diagram is the multiple separate vtable entries for the virtual destructor. In practice, there will be a single entry that would point to ~A(), ~B() and ~AB() for A, B and AB() respectively.

For example, if I compile your code using gcc and examine the assembly, I see the following code in ~AB():

LEHE0:         movq    -24(%rbp), %rax         addq    $16, %rax         movq    %rax, %rdi LEHB1:         call    __ZN1BD2Ev LEHE1:         movq    -24(%rbp), %rax         movq    %rax, %rdi LEHB2:         call    __ZN1AD2Ev 

This calls ~B() followed by ~A().

The virtual tables of the three classes look as follows:

; A __ZTV1A:         .quad   0         .quad   __ZTI1A         .quad   __ZN1AD1Ev         .quad   __ZN1AD0Ev  ; B __ZTV1B:         .quad   0         .quad   __ZTI1B         .quad   __ZN1BD1Ev         .quad   __ZN1BD0Ev  ; AB __ZTV2AB:         .quad   0         .quad   __ZTI2AB         .quad   __ZN2ABD1Ev         .quad   __ZN2ABD0Ev         .quad   -16         .quad   __ZTI2AB         .quad   __ZThn16_N2ABD1Ev         .quad   __ZThn16_N2ABD0Ev 

For each class, entry #2 refers to the class's "complete object destructor". For A, this points to ~A() etc.

like image 137
NPE Avatar answered Sep 22 '22 01:09

NPE