4

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

4

3 回答 3

7

RTTI 可能对您没有帮助。RTTI 仅在类具有虚拟方法时才有效,并且并非所有分配都是具有虚拟方法的对象。

您真正需要做的是有一些方法将堆栈跟踪附加到您的分配中。然后,您可以获得有关内存分配位置的信息。如果是泄漏内存的对象,您将寻找一个类构造函数。

无论如何,那里有这样的东西吗?是的。一个免费的 Windows 库是Visual Leak Detector。有更多功能齐全的商业产品(如 Bounds Checker 和 IBM 的 Rational Purify),但 VLD 效果很好。它无数次帮助我发现内存泄漏。

于 2008-12-09T08:15:48.643 回答
6

我使用 valgrind --leak-check=full,它会给我每个泄漏块的分配站点的堆栈跟踪。这些信息比类型信息更有用 valgrind(发音像“Val grinned”)摇滚

于 2008-12-09T08:17:58.047 回答
1

像诺曼一样,我提倡valgrind。这对于分析堆栈跟踪确实是一个很好的建议!

我在 KDevelop 中使用它。

于 2008-12-09T12:27:08.213 回答