Undefined behaviour with non-virtual destructors - is it a real-world issue?
- by Roddy
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?