# My destructor's not getting called

This topic is 5462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a base class A and a class B that derives from A. I do not define a destructor in A but I do define a virtual destructor in B. I then instantiate an instance of B as follows: A* obj = new B But when I come to delete it: delete obj it doesn't call the destructor I define in B. I'm hoping I don't have to do something as dirt as: delete (B*)obj If the C++ compiler allows for virtual functions, why can't the same be done for destructors?

##### Share on other sites
C++ does support virtual destructors, you just haven't provided one. Create a virtual destructor for A, and all your problems should be solved.

The problem is that, because you don't explicitly define ~A to be virtual, the compiler doesn't allow ~B to overwrite the default ~A [there is a destructor, you just don't write it]. So when you delete a pointer to A, it can't know to call ~B. That probably makes no sense at all, but there you go.

CM

##### Share on other sites
That makes perfect sense. Thanks a lot

##### Share on other sites
Actually, I have another question:

Say you have defined a virtual destructor in class A and B, how can you ensure when deleting a B object, that first class B's destructor gets called, and then A's destructor gets called?
Or is this not possible?

##### Share on other sites
That is automaticly done.
Just write a small test programm to see it yourself.

##### Share on other sites
Just tried it and you're right. Thanks again

##### Share on other sites
Quote:
 Original post by Conner McCloudC++ does support virtual destructors, you just haven't provided one. Create a virtual destructor for A, and all your problems should be solved.The problem is that, because you don't explicitly define ~A to be virtual, the compiler doesn't allow ~B to overwrite the default ~A [there is a destructor, you just don't write it]. So when you delete a pointer to A, it can't know to call ~B. That probably makes no sense at all, but there you go.CM

Just as an open question, why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class? (And in that case, shouldn't the compiler be able to optimize out the virtuality?)

##### Share on other sites
Quote:
 Original post by ZahlmanJust as an open question, why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class? (And in that case, shouldn't the compiler be able to optimize out the virtuality?)

When you don't want a vtbl pointer in your class (e.g. 3D math vector class). If you don't need polymorphism, you shouldn't have to pay for it. And even though the virtuality may be optimized away when the compiler knows the actual type of the instance (i.e. not a pointer or a reference), the fact that the class does now have a virtual function (and thus needs a vtbl) cannot.

##### Share on other sites
If I recall correctly, virtual functions require more memory per object instance than non-virtual functions, because the function may reside in a different memory location per instance instead of per object. Therefore, if you really needs the memory, you don't want a virtual destructor.

Hope this helps! [smile]

##### Share on other sites
Quote:
 Original post by Zahlmanwhy would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class?
When you know that no objects of classes that subclass that class will ever have
delete
called on their base class pointers, and you want to maximize performance for calling
delete
on the derived class pointers. Also, if you have no other virtual functions (probably because you don't intend any subclassing) it'll save you a few bytes and a few constructor instructions for the vtable.

I've only ever seen the former case once, in a trivial bit of code that really didn't need the speed boost. I make a habit of declaring a virtual destructor whenever subclassing will be done.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 16
• 13
• 14