EDIT: topic subject should be "...inheriting objects." (How do I change the subject line?)
Question: Why did I not get memory leaks when deleting a base class pointer? Please note that the code below is poorly formed and I know that. My question is "why did it work anyway?"
Background:
Using VS2013 Express for Desktop, Windows 7, I am programming a graphics editor in C++ and DirectX 9. I had a situation similar to:
class UndoRedo
{
UndoRedo();
~UndoRedo(); // deletes pointers in actions
std::vector<BaseClass*> actions;
// other functions, e.g., rule of three, etc.
};
class BaseClass
{
virtual void Undo() { }
virtual void Redo() { }
};
class Derived : BaseClass
{
Derived( int inX, int inY, int inZ ) : x(inX), y(inY), z(inZ) { }
void Undo();
void Redo();
// e.g.,
int x, y, z;
};
Implementation of actions was initiated as:
Derived* derived = new Derived( ... data ... );
UndoRedoPtr->actions.push_back( (BaseClass*)derived );
The destructor for UndoRedo:
for( size_t i = 0; i < actions.size(); i++ ) delete actions[i];
Why are there no memory leaks, considering that the allocation for Derived should be larger than for BaseClass?
I eventually DID create another derived class, the deletion of which DID result in a memory leak. That was resolved by changing BaseClass to:
class BaseClass
{
BaseClass() { }
virtual ~BaseClass() { }
virtual void Undo() { }
virtual void Redo() { }
private:
EditAction(const EditAction&) { }
EditAction& operator= (const EditAction&) { }
};
In spite of my original best efforts to incorrectly implement BaseClass, the process did not result in memory leaks for any but the last derived class I added.
So, again, why did I not get memory leaks when deleting BaseClass* without a BaseClass virtual destructor when that pointer was, in fact, Derived* ?
EDIT: Is there, e.g., a minimum ("new") allocation block which gets deleted whether the entire allocation is used or not?