在 C++ 中查找未知对象的类型

发布于 2024-07-10 13:58:13 字数 225 浏览 5 评论 0原文

有很多方法可以检查程序是否存在内存泄漏。 您最终会得到指向泄漏内存块的指针列表,但是有没有一种好方法可以找到每个块的更多信息? 例如:如果我知道该对象是一个字符串,则实际的字符串值可以使查找泄漏变得更加容易。

RTTI 是否存在后门可以实现这一点?

要解决的问题是,当您获得指针时,运行时系统已经处于关闭状态,并且您获得原始内存块指针而不是指向对象的指针(尽管在许多情况下可能是相同的)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2024-07-17 13:58:13

RTTI 可能帮不了你。 RTTI 仅在类具有虚拟方法时才起作用,并且并非所有分配都是具有虚拟方法的对象。

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

无论如何,外面有这样的事情吗? 是的。 适用于 Windows 的免费库是 Visual Leak Detector。 还有功能更齐全的商业产品(例如 Bounds Checker 和 IBM 的 Rational Purify),但 VLD 效果很好。 它无数次帮助我发现内存泄漏。

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.

思念绕指尖 2024-07-17 13:58:13

我使用 valgrind --leak-check=full,这将为我提供每个泄漏块的分配站点的堆栈跟踪。 此信息比单纯的类型信息方式更有用。 valgrind(发音像“Val grinned”)摇滚

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!

情域 2024-07-17 13:58:13

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

我在 KDevelop 中使用它构建。

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

I use it build in KDevelop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文