orezvani
orezvani

Reputation: 3775

Delete all the memory allocated to an object

I am writing a program which creates thousands of instances of a class that I borrowed from somebody else's code. For example I have a class B,

class B {
  B(int a);
  int some_function();
  ...
};

In my program, I create objects of this class:

int main() {
  for(int i=0; i<10000; i++) {
    B *b = new B(i);
    b->some_function();
    delete(b);
  }
}

But class B has memory leak and fills my RAM after a few iterations. Is there any way to remove all the memory allocated to this class after each iteration of my program?

P.S. the class is way more complicated than this example, so I have considered this option prior to debugging that class.

Upvotes: 0

Views: 45

Answers (2)

Orelsanpls
Orelsanpls

Reputation: 23515

Maybe the class B inherit from some other class and destructor is not virtual. Can you check it out?

EDIT:

To check out leaks i recommand to install valgrind (software).

How install? in ubuntu, sudo apt-get install valgrind

How use it? Simply launch your program with it : valgrind ./yourProgram

How use it for leaks detections? valgrind --leak-check=yes ./yourProgram

Here is a guide

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385104

No, there isn't. You'll have to fix that class.

Upvotes: 4

Related Questions