Jump to content
  • Advertisement
Sign in to follow this  
badmoon

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
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


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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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?)


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
why 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!