C++ Memory not freed when deleting pointer to subclass
c++, dynamic-memory-allocation, inheritance
Solution
In C++, constructors are called upwards in the hierarchy, that is, when you create `Derived`, `Base()` is executed before `Derived()`. This means that you are running `objPtr = new SomeObject;` twice, and only deleting it once.
You should also make your base class destructor virtual, especially if you will ever delete `Derived` instances from a `Base` ptr.
Problem
I am using dynamic memory allocation in my code, and am running into issues when trying to delete a pointer to a subclass. I find that the memory originally allocated is not freed when I use the `delete` keyword. The functionality works fine with the original base class. This is an issue because I'm running the code on an arduino and the RAM gets eaten up quickly then crashes. Here's some example code: ``` class Base { public: Base(){ objPtr = new SomeObject; } ~Base(){ delete objPtr; } SomeObject* objPtr; }; class Sub : public Base { public: Sub(){ objPtr = new SomeObject; } }; // this works fine int main() { for (int n=0;n<100;n++) // or any arbitrary number { Base* basePtr = new Base; delete basePtr; } return 0; } // this crashes the arduino (runs out of RAM) int main() { for (int n=0;n<100;n++) // or any arbitrary number { Sub* subPtr = new Sub; delete subPtr; } return 0; } ``` I imagine it has something to do with the syntax of the destructor in the base class. Even if I make a custom destructor for the subclass, the same issues occur. Any ideas?