views:

332

answers:

3

There are many ways to check programs for memory leaks. You end up with that list of pointers to leaked memory blocks, but is there a good way to find out more information for each block? For example: if I know that the object was a string, the actual string value could make finding the leak a lot easier.

Is there a backdoor into RTTI that makes that possible?

Problems to solve would be that by the time you get the pointers the runtime system is already in a state of shutdown and you get raw memory block pointers instead of pointers to objects (though in many cases that might be the same).

+6  A: 

RTTI may not help you. RTTI only works if the classes have virtual methods, and not all allocations are of objects with virtual methods.

What you really need to do is have some way to attach a stack trace to your allocations. Then you can get information about where the memory was allocated. You'd look for a class constructor if it was objects that leaked memory.

Anyway, is there something like this out there? Yes. A free library for Windows is Visual Leak Detector. There are more fully featured commercial products (like Bounds Checker, and IBM's Rational Purify), but VLD works great. It's helped me countless times spot memory leaks.

Kevin
+5  A: 

I use valgrind --leak-check=full, which will give me a stack trace of the allocation site of every leaked block. This information is way more useful than just type information. valgrind (pronunced like "Val grinned") rocks!

Norman Ramsey
A: 

like norman i advocate for valgrind. It is really a good advice for analyzing the stack trace!

I use it build in KDevelop.

joki