Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

G++ Multi-platform memory leak detection tool

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.

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.

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.

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.

like image 768
mnuzzo Avatar asked Jun 27 '09 05:06

mnuzzo


People also ask

What is the best tool to detect memory leaks?

To find memory leaks and inefficient memory usage, you can use tools such as the debugger-integrated Memory Usage diagnostic tool or tools in the Performance Profiler such as the . NET Object Allocation tool and the post-mortem Memory Usage tool.

Can Sonarqube detect memory leak?

There aren't any built-in rules that generically detect memory leaks.

Can I use valgrind on Windows?

Valgrind Memcheck is a tool for detecting memory-usage problems such as leaks, invalid memory access, incorrect freeing, and referencing undefined values. Valgrind integration in CLion works on Linux, macOS, and Windows via WSL (see Valgrind on WSL).


2 Answers

Valgrind is your best friend. Valgrind has a plugin for 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 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.

like image 108
LiraNuna Avatar answered Oct 02 '22 15:10

LiraNuna


In newer versions of gcc there is something called leak sanitizer. You just have to add -fsanitize=leak to 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).

like image 33
bebidek Avatar answered Oct 02 '22 14:10

bebidek