Memory dealocation / leakage problem (urgent please)

Hi,

Dear i need your suggestion as soon as possible to complete the tests on my data before monday.

Senario:

-I developed a project which contains 4 classes
-I create the object of the class with new operator and later use Delete operator to release the memory.
- within the classes to call functions i use call by reference to avoid the copy of the variables.
- I stored the intermediate results in a text file which i close later and open it wiht append mode.
-My project run iteratively and perform same statistical test on the data several time. During each iteration i store some static variables to reuse in the next iteration.
1
2
3
 static std::map <int, CPCSteps> bestKvariables;
static std::map <int, std::vector<int> > cpcPath;
static int nmFunctionCalls = 0, totalNmOfFunctionCalls=0 , nmTOCOnonFired=0, nmTOCOFired=0;



Problem:
As there are may be 200 iterations, i monitor the memory used by my project which is always increment by about 100MB on each iteration and later give an error of memory.

Interesting point: I observed that if i execute the same project on the same dataset but not incrementally just once (process data in a single iteration ) then there is no problem and even there is no change of the memory as it change on each iteration by 100MB in incremental execution of the same project at the same data set.

Please i need your valueable suggestion to reduce the memory leakage and to dealocate the memory.

Thanks in advance.



Last edited on
-I create the object of the class with new operator and later use Delete operator to release the memory.


Try using smart pointers wherever you have new/delete stuff and see if that fixes your problem.
Dear thank you for your reply, Can you give me an example how i can use smart pointers.

I also try by assigning Null value to the object and then i called delete operator. But unfortunately still problem is there.

classA objA = new classA();

--
--
--

objA = NULL;
delete objA;
That is bad, if you assign NULL to it before you deleting it you will leak memory.

As for how to use smart pointers, I would have a look at std::auto_ptr's or boost's smart_ptr library's documentation.
Now i test boost's smart_ptr library

boost::scoped_ptr<MMPCbar> objMMPCbar(new MMPCbar);

and the same for each object of the class.

But the memory problem is still there.
ok, you probably don't want to hear this, but the only way to give you a chance to make progress on this is to break it down into smaller pieces

1. write unit-test/test drivers to test as small of piece of your program at a time. even if that means just instantiating an automatic variable and letting it fall out of scope
2. run valgrind on your test program (or your favorite memory testing program)
http://valgrind.org/
3. if it passes, add another test that covers a little bit more and goto 2
4. if it doesn't pass, debug, using a debugger if needed, and fix before you go on

in particular, for C++, I recommend checking your assignment operators and copy-constructors, keeping in mind that they could be automagically defined by the compiler for you

also, I would avoid added boost's smart pointers, for now - this will likely just add complexity to your debugging without solving your problems

simplify, divide, and debug

Last edited on
Thank you kfmfe04, good option but unfortunately "Valgrind" is not for windows platform.

The Valgrind 3.5.X series supports the following platforms.

x86/Linux: support is mature and almost complete.
AMD64/Linux: support is mature and almost complete.
PPC32/Linux: support is new but fairly complete.
PPC64/Linux: support is new but fairly complete.
x86/Darwin (Mac OS X): support is new.
AMD64/Darwin (Mac OS X): not officially supported, but probably works.
Topic archived. No new replies allowed.