Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the type of an unknown object in C++

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).

like image 955
Hans Avatar asked Dec 09 '22 22:12

Hans


1 Answers

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.

like image 125
Kevin Avatar answered Dec 29 '22 13:12

Kevin