How to protect code from memory leaks

By ArtMusic ·
How to protect code from memory leaks (C, C + +)?
What tools for debugging and testing are you using?
I found a few tricks, but it does not solve the problem completely!

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

How about smart pointers?

by Robert Miles In reply to How to protect code from ...

How about smart pointers? I find them very useful when coding.

Collapse -


by Hooking In reply to How to protect code from ...

The techniques I use heavily are reference-counted objects with smart pointers, STL containers, and when using Qt it can be convenient to subclass QObject and use Qt's parent/child system to do the cleanup for you. I haven't written an explicit delete statement in years, except when working with low-level image buffers (which are always wrapped in a reference-counting shell anyway).

I should add that memory management should be an explicit design goal, and you should design your system with it in mind. Retrofitting an existing system will only get you so far.

Collapse -

some errors?

by ArtMusic In reply to How to protect code from ...

"In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic memory management or bounds checking"
Hi! Smart pointers in my opinion is very relevant. I would like to know Are there any errors with automatic memory management?

Collapse -

some tools

by Hooking In reply to How to protect code from ...

I do not have much experience with smart pointers. As for me, I use vld or deleaker for debugging. Yet I use cppcheck (tool for C++)

Collapse -

Things You'll Need

by Robert Miles In reply to How to protect code from ...

Things You'll Need

Proficiency in C++
C++ compiler
Debugger and other investigative software tools
Understand the operator basics. The C++ operator "new" allocates heap memory. The "delete" operator frees heap memory. For every "new," you should use a "delete" so that you free the same memory you allocated:

char* str = new char [30]; // Allocate 30 bytes to house a string.

delete [] str; // Clear those 30 bytes and make str point nowhere.
Reallocate memory only if you've deleted. In the code below, str acquires a new address with the second allocation. The first address is lost irretrievably, and so are the 30 bytes that it pointed to. Now they're impossible to free, and you have a memory leak:

char* str = new char [30]; // Give str a memory address.

// delete [] str; // Remove the first comment marking in this line to correct.

str = new char [60]; /* Give str another memory address with
the first one gone forever.*/

delete [] str; // This deletes the 60 bytes, not just the first 30.
Watch those pointer assignments. Every dynamic variable (allocated memory on the heap) needs to be associated with a pointer. When a dynamic variable becomes disassociated from its pointer(s), it becomes impossible to erase. Again, this results in a memory leak:

char* str1 = new char [30];

char* str2 = new char [40];

strcpy(str1, "Memory leak");

str2 = str1; // Bad! Now the 40 bytes are impossible to free.

delete [] str2; // This deletes the 30 bytes.

delete [] str1; // Possible access violation. What a disaster!
Be careful with local pointers. A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you don't delete it, it will persist after the program exits from the function:

void Leak(int x){
char* p = new char [x];
// delete [] p; // Remove the first comment marking to correct.
Pay attention to the square braces after "delete." Use "delete" by itself to free a single object. Use "delete" [] with square brackets to free a heap array. Don't do something like this:

char* one = new char;
delete [] one; // Wrong
char* many = new char [30];

delete many; // Wrong!
If the leak yet allowed - I'm usually seeking it with deleaker

Collapse -


by ArtMusic In reply to How to protect code from ...

Thank you for sharing your methods. I think that using smart pointers - clever. As for the debugger, it is quite difficult to choose a good one.

Related Discussions

Related Forums