Quote:Original post by alvaro
The destructor should be virtual in the base class. That way the correct destructor can be called. Calling the destructor for base class on an instance of a derived class is probably undefined behavior, so crashing is normal.
The normal behavior it to simply run through the destructor you explicitly called (and it's parents destructors). For example if there was no virtual destructor in that chain and my pointer was a VirtualTest2 it would call VirtualTest2 dtor then VirtualTest1 dtor. This is why virtual destructors were invented - so you can call the dtor from anywhere in the polymorphic chain and destruct the entire chain.
Quote:Original post by rip-off
I couldn't reproduce the crash on either MSVC or GCC, using a minimal program wrapped around your code.
Still, deleting a type through a pointer to a Base with a non-virtual destructor is bound to be undefined behaviour, so it could crash on your machine. Though I cannot see how most common C++ implementations would generate code that would crash with your example.
What compiler did you use, and did you change any interesting flags?
VS2008 and 2003 on 2 different machines. The behavior I would expect is VirtualTest1 dtor being called, and that's it, however something is happening with the virtual method table to complicate the case.