Undefined behaviour with non-virtual destructors - is it a real-world issue?

Posted by Roddy on Stack Overflow See other posts from Stack Overflow or by Roddy
Published on 2010-12-23T22:43:27Z Indexed on 2010/12/23 22:54 UTC
Read the original article Hit count: 185

Consider the following code:

class A 
{
public:
  A() {}
  ~A() {}
};

class B: public A
{
  B() {}
  ~B() {}
};

A* b = new B;
delete b; // undefined behaviour

My understanding is that the C++ standard says that deleting b is undefined behaviour - ie, anything could happen. But, in the real world, my experience is that ~A() is always invoked, and the memory is correctly freed.

if B introduces any class members with their own destructors, they won't get invoked, but I'm only interested in the simple kind of case above, where inheritance is used maybe to fix a bug in one class method for which source code is unavailable.

Obviously this isn't going to be what you want in non-trivial cases, but it is at least consistent. Are you aware of any C++ implementation where the above does NOT happen, for the code shown?

© Stack Overflow or respective owner

Related posts about c++

Related posts about destructor