Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,104 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,690 people online right now. Registration is fast and FREE... Join Now!




Looking for Memory Leak Advice

 
Reply to this topicStart new topic

Looking for Memory Leak Advice

complete
16 Oct, 2007 - 02:26 PM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 22


My Contributions
Apart from buying BoundsChecker or any of the other tools to track down memory leaks. Does anyone have any advice or tips from your years of programming experience to offer on how to track down the causes of memory leaks?

I find that the big dump that happens at the end of a program does not typically lend any useful clues as to where or what was allocated that was not freed. I find that I have to back track and comment out sections of code until through the process of elimination and trial-and-error, I am able to narrow down where the problem is. And since this is usually at the bottom of the To-Do list, the memory leaks are not plugged.

This is a common problem. So I did search before I made this post. There were specific memory leak issues but nothing general like this.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Looking For Memory Leak Advice
16 Oct, 2007 - 09:53 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The best way to prevent memory leaks to is use "defensive programming" from the very start. Always keep track of your pointers and what is being altered along the way. Delete what you created and initialize rather than just leaving empty until use.

But if you find that you have a leak or debugging someone else's code find where there is a bunch of pointer arithmetic and start your search there. A lot of leaks come from pointer use (which is why they have been phased out in later languages and why garbage collection has been implemented).

Match up your use of "new" with your use of "delete". Every object you declare do you cleanup? Are you using your destructors properly? Are you creating an object pointer and letting the object go out of scope so that your pointer is dangling?

Steps I use...

1) Start with your pointers - make sure you initialize them to null or an object. Make sure that object won't go out of scope on you. Do you have any complex pointer handling (like a node list?) your problems are probably going to be there.

2) Cleanup cleanup cleanup... use your destructors properly, are you effectively using delete? Are you recreating an object over and over again instead of deleting the old copies?

3) Always check the bounds of your arrays and make sure there is no way to write out of those bounds. You can do this as part of your number 4 tip....

4) Unit test your functions in a driver program and see if the leak exists there.

But the best tip I can tell you is to use good habits when initially coding a function and clean up resources as soon as you possibly can instead of saying "I will clean that up later" because you may forget all about it.

Hope this helps! smile.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month