Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing memory leaks

Status
Not open for further replies.

titanzero

Programmer
May 3, 2007
30
0
0
EU
Hi everybody

I am writing a console app in C++ under XP and I would like to be able to test for memory leaks.

In my program I create a number of variables and objects on the free store but I am unsure if my code cleans them up correctly.

I've been using the Processes tab under Task Manager to keep track of how much memory is being used through out the execution of my app. I've noticed that when I create a new variable the Mem Usage increases but when I delete that same variable it does not decrease. This has led me to believe I have a memory leak. However, when I create a second variable of the same type the Mem Usage does not increase nor does it decrease when I delete that variable. This is confusing me greatly - can anybody point me in the direction of a better (and if possible free) tool for keeping track of what memory my app is using as it runs. Or can anybody point me in the direction of some code I can place in my app to report the amount of memory it is using.

I've included a little test program I've written to see how Task Manager reports memory usage and to better explain what i mean.

CODE PORTION---------------------
int* pnumber;

pnumber = new int[10];
//Mem usage increases by 4k

delete pnumber;
//Mem usage stays the same (would expect it to decrease)

pnumber = new int[10];
//Mem usage stays the same (would expect it to increase)

delete pnumber;
//Mem usage stays the same (would expect it to decrease)

cout << endl << endl << "END OF PROG";
cout << endl;
return 0;
END-----------------------

Thank you for your time
Andrew
 
I have only partial answer now: you can't detect C++ application memory leaks with Task Manager data only because of C++ run-time library gets heap memory from OS then allocates and deallocates objects in this memory chunk(s) "logically", with its own methods. From "OS point of view" it's allocated storage and it's what you see in task manager window.
 
Thanks for the info - that explains why I get unexpected results from Task Manager.

Thanks again
Andrew
 
You might want to look into using smart pointer objects that automatically delete pointers for you.
C++ comes with the auto_ptr class, but it can't be used with arrays, can't be copied or passed between functions, so it's not good for all types of pointers. The Boost library ( has some good smart pointers that use reference counting and only delete themselves when the last copy of the smart pointer goes out of scope.
 
It's all cool now. It turns out I do not have a memory leak and the methods I am using to create and destroy my objects are safe.

In the end I ran my program lots of times (some 10000000 times) and at no point did it start eating memory. I'm 90% sure that my app is not leaking.

I looked at the auto_ptr class in my reference book but as they can not be used with arrays it was not a suitable solution to my problem, but thank you all the same.

Again thanks for you time
Andrew
 
I wrote my own AutoPtr class a while ago which does support arrays and any other custom type of destruction like using free() on memory that you malloc()... It's not reference counted though, so you can't store it in an STL container or have multiple copies of it.

Here's where you can find it:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top