5

This is just an idle thought I had when reading this other question:

What is the correct way to delete char**

If the chars mentioned in that question had been created inside an object and that object was deleted, would that correctly clean up the pointers as well, or would they be stuck in memory?

Community
  • 1
  • 1
shieldfoss
  • 886
  • 2
  • 12
  • 22

6 Answers6

6

If you delete an object, the destructor for that object will be called, so you need to do a delete in the destructor. So remember, everything that the class has allocated on the heap has to be freed in the destructor. If it was been allocated on the stack this happens automatically

struct A
{
    A() { std::cout << "A()" << std::endl; ptr = new char[10]; }
    ~A() { std::cout << "~A()" << std::endl; delete []ptr; }
    char *ptr;
};

But be careful, if you are using inheritance, if A inherits from a base class, you need to make the base destructor virtual, or else the destructor in A will not be called and you will have a memory leak.

struct Base
{
    virtual ~Base() {}
};

struct A : public Base
{
    A() { std::cout << "A()" << std::endl; ptr = new char[10]; }
    ~A() { std::cout << "~A()" << std::endl; delete []ptr; }
    char *ptr;
};
hidayat
  • 9,493
  • 13
  • 51
  • 66
3

They would stuck in the memory. Therefore you need to define Destructor when you make dynamic memory allocation within objects. The D'tor is called when the parent object is about to be delete, and you should explicitly free in the D'tor all the sub-allocated memory.

For more info, look at Wikipedia.

Yaakov Shoham
  • 10,182
  • 7
  • 37
  • 45
  • 3
    And when you define a destructor, you should define a copy-constructor and a copy-assignment operator too. [See here](http://stackoverflow.com/questions/4172722/). – Björn Pollex May 13 '11 at 09:19
  • 1
    @Space: Wrong...Rule of three doesn't include *constructor* : [See wiki](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) – Nawaz May 13 '11 at 09:21
  • @Nawaz: Thanks. That was a typo. Better now? – Björn Pollex May 13 '11 at 09:23
  • @Space: Again, No. You need rule of three only when you define any of the three : destructor OR copy constructor OR copy assignment operator. If you define ONE, you've to define ALL. – Nawaz May 13 '11 at 09:24
1

Yes, you would do the clean up of memory in class's destructor.

cpx
  • 17,009
  • 20
  • 87
  • 142
1

The pointer has to be delete explicitly (by you or some library wrapper). For example;

struct A {
  char *p; // assume p = new char[] somewhere;
};

A* pA = new A;
delete pA; // <-- this doesn't clean up char* p
iammilind
  • 68,093
  • 33
  • 169
  • 336
1

The object would only clean up the pointers if you specifically told it to in the Deconstructer.

Jordonias
  • 5,778
  • 2
  • 21
  • 32
1

If a class is correctly written, deleting the an instance of that class should release all the resources allocated by that class.

If the class stores pointers to memory that it did not allocate, unless documentation specifies otherwise, it is generally not expected to delete those pointers.

idz
  • 12,825
  • 1
  • 29
  • 40