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?
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.
You should make the base class' destructor virtual.
virtual ~Base(){
delete objPtr;
}
Sub::Sub() constructor allocates a second extra SomeObject after a first one was allocated by Base::Base() super constructor and the second allocated pointer is assigned to objPtr provoking a leak.
Note: Base::Base() is implicitly called by Sub::Sub()
Solution: just remove needless allocation in Sub::Sub()
Another suggestion: make your Base destructor virtual as is recommended with inheritance
virtual ~Base(){
delete objPtr;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With