Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Memory not freed when deleting pointer to subclass

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?

like image 486
ryantuck Avatar asked Mar 22 '13 01:03

ryantuck


3 Answers

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.

like image 93
congusbongus Avatar answered Oct 30 '22 05:10

congusbongus


You should make the base class' destructor virtual.

virtual ~Base(){
    delete objPtr;
}
like image 41
TieDad Avatar answered Oct 30 '22 07:10

TieDad


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;
}
like image 1
thierry.bravier Avatar answered Oct 30 '22 07:10

thierry.bravier