eclipse G++ 多平台内存泄漏检测工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1052145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:50:16  来源:igfitidea点击:

G++ Multi-platform memory leak detection tool

c++eclipsememory-managementmemory-leaks

提问by mnuzzo

Does anyone know where I can find a memory memory leak detection tool for C++ which can be either run in a command line or as an Eclipse plug-in in Windows and Linux. I would like it to be easy to use. Preferably one that doesn't overwrite new(), delete(), malloc() or free(). Something like GDB if its gonna be in the command line, but I don't remember that being used for detecting memory leaks. If there is a unit testing framework which does this automatically, that would be great.

有谁知道我在哪里可以找到 C++ 的内存泄漏检测工具,它可以在命令行中运行,也可以作为 Windows 和 Linux 中的 Eclipse 插件运行。我希望它易于使用。最好不要覆盖 new()、delete()、malloc() 或 free()。像 GDB 这样的东西,如果它会出现在命令行中,但我不记得它用于检测内存泄漏。如果有一个自动执行此操作的单元测试框架,那就太好了。

This question is similar to other questions (such as Memory leak detection under Windows for GNU C/C++) however I feel it is different because those ask for windows specific solutions or have solutions which I would rather avoid. I feel I am looking for something a bit more specific here. Suggestions don't have to fulfill all requirements, but as many as possible would be nice.

这个问题类似于其他问题(例如Windows 下的 GNU C/C++ 内存泄漏检测)但是我觉得它不同,因为那些要求 Windows 特定的解决方案或有我宁愿避免的解决方案。我觉得我在这里寻找更具体的东西。建议不必满足所有要求,但尽可能多就好了。

Thanks.

谢谢。

EDIT: Since this has come up, by "overwrite" I mean anything which requires me to #include a library or which otherwise changes how C++ compiles my code, if it does this at run time so that running the code in a different environment won't affect anything that would be great. Also, unfortunately, I don't have a Mac, so any suggestions for that are unhelpful, but thank you for trying. My desktop runs Windows (I have Linux installed but my dual monitors don't work with it) and I'd rather not run Linux in a VM, although that is certainly an option. My laptop runs Linux, so I can use that tool on there, although I would definitely prefer sticking to my desktop as the screen space is excellent for keeping all of the design documentation and requirements in view without having to move too much around on the desktop.

编辑:既然已经出现了,“覆盖”我的意思是任何需要我#include 一个库或者改变 C++ 编译我的代码的方式,如果它在运行时这样做,以便在不同的环境中运行代码不会影响任何很棒的事情。另外,不幸的是,我没有 Mac,因此任何建议都无济于事,但感谢您的尝试。我的桌面运行 Windows(我安装了 Linux,但我的双显示器不能使用它),我宁愿不在 VM 中运行 Linux,尽管这当然是一种选择。我的笔记本电脑运行 Linux,所以我可以在那里使用该工具,尽管我绝对更喜欢坚持使用我的桌面,因为屏幕空间非常适合保持所有设计文档和要求,而不必在桌面上移动太多.

NOTE: While I may try answers, I won't mark one as accepted until I have tried the suggestion and it is satisfactory.

注意:虽然我可以尝试答案,但在我尝试了建议并且令人满意之前,我不会将其标记为已接受。

EDIT2: I'm not worried about the cross-platform compatibility of my code, it's a command line application using just the C++ libraries.

EDIT2:我不担心我的代码的跨平台兼容性,它是一个仅使用 C++ 库的命令行应用程序。

回答by LiraNuna

Valgrindis your best friend. Valgrind has a pluginfor eclipse. "Sadly" Valgrind does not run on Windows, but it runs on Mac OSX, *BSD and Linux, so I'd consider that "multi-platform". :)

Valgrind是您最好的朋友。Valgrind 有一个Eclipse插件。“遗憾的是”Valgrind 不能在 Windows 上运行,但它可以在 Mac OSX、*BSD 和 Linux 上运行,所以我认为它是“多平台的”。:)

Valgrind does "overwrite" new/delete/malloc/free but not during compilation (so you don't have to recompile if that's what you mean). It interprets the binary so the performance suffer a bit during testing.

Valgrind 确实“覆盖”了 new/delete/malloc/free 但不是在编译期间(所以如果这就是你的意思,你不必重新编译)。它解释二进制文件,因此在测试期间性能会受到影响。

回答by bebidek

In newer versions of gcc there is something called leak sanitizer. You just have to add -fsanitize=leakto compile command. Then you run your program normally and at the end, if there was any leak, you'll get summary (in terminal of course).

在较新版本的 gcc 中,有一种叫做leak sanitizer 的东西。你只需要添加-fsanitize=leak编译命令。然后你正常运行你的程序,最后,如果有任何泄漏,你会得到摘要(当然是在终端中)。

回答by David Cournapeau

for 32 bits applications, valgrind + wine can be a working solution as well for windows apps. If your app can be run under wine without any change, then running it under valgrind + wine works well in my (quite limited) experience. Since your app works under Linux, I would guess that your app is likely to run under wine (avoids very windows specific code which is not yet supported in wine).

对于 32 位应用程序,valgrind + wine 也可以作为 Windows 应用程序的工作解决方案。如果您的应用程序可以在没有任何更改的情况下在 wine 下运行,那么在我(相当有限的)经验中,在 valgrind + wine 下运行它效果很好。由于您的应用程序在 Linux 下运行,我猜您的应用程序很可能在 wine 下运行(避免在 wine 中尚不支持的非常 Windows 特定的代码)。

回答by codeincarnate

While you mentioned that this isn't preferred the Boehm Garbage Collectorcan be used to detect memory leaks. Simply put, if the garbage collector is ever actually running then generally you have a memory leak. It's used this way by Mozilla to detect memory leaks in Firefox.

虽然您提到这不是首选,但Boehm 垃圾收集器可用于检测内存泄漏。简而言之,如果垃圾收集器确实在运行,那么通常会出现内存泄漏。Mozilla 使用这种方式来检测 Firefox 中的内存泄漏。

回答by Daniel Trebbien

DUMAis a cross-platform leak detection library which I use for many of my projects. It's nice because you don't have to #includeany DUMA-specific header, but just link in the library before you link in your system's libc, which contains the memory allocation routines, and after linking in libstdc++.

DUMA是一个跨平台的泄漏检测库,我用于我的许多项目。这很好,因为您不需要#include任何特定于DUMA 的头文件,而只需在链接到包含内存分配例程的系统 libc 之前以及链接到 libstdc++ 之后链接到库中。

It can be kind of tricky to set up, especially when used with C++ projects, but I think that it is well worth the time invested. It has helped me to find a few memory leaks before that I might not have discovered otherwise, and one case where I deleted an allocation twice.

设置起来可能有点棘手,尤其是在与 C++ 项目一起使用时,但我认为花时间投入是非常值得的。它帮助我找到了一些之前我可能没有发现的内存泄漏,还有一种情况是我删除了两次分配。

One note: it's much easier if you build a static archive (built by default on Windows) because it helps to reduce "false positives" that are actually caused by leaky runtimes.

一个注意事项:如果您构建一个静态存档(默认在 Windows 上构建)会容易得多,因为它有助于减少实际上由泄漏运行时引起的“误报”。

回答by olliej

There's the leakstool on MacOS X (i don't know if it exists elsewhere) and i used to use a tool called memprof on linux. There's also valgrind which works on linux and is incredibly awesome but has a substantial performance hit, and ostensibly on macos but i haven't tried it.

leaksMacOS X 上有这个工具(我不知道它是否存在于其他地方),我曾经在 linux 上使用一个名为 memprof 的工具。还有 valgrind 可以在 linux 上运行,非常棒,但性能受到很大影响,表面上是在 macos 上,但我还没有尝试过。

I am unaware of any such tools on windows however.

但是,我不知道 Windows 上有任何此类工具。