G++ Multi-platform memory leak detection tool - c++

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.

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.

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

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

DUMA is a cross-platform leak detection library which I use for many of my projects. It's nice because you don't have to #include any 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++.
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.
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.

There's the leaks tool 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.
I am unaware of any such tools on windows however.

While you mentioned that this isn't preferred the Boehm Garbage Collector can 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.

Related

How to Detect Memory leaks for Qt windows desktop application, using QtCreator [duplicate]

Lately I have been developing in C++ with QT Creator. All is well and I'm nearly at the point of packaging and distributing my application. But obviously before any release you better make sure you have everything right. So I'm at the testing stage, and something tells me that I have some slight memory issues. Nothing serious, but I like to obsess over these ;-).
So I decided to try some memory leak detection libraries. For starters I looked at this question. I disregarded Purify and Insure++ because of the costs. Someone else suggested to use the MSVC compiler so I could use the CRT memory validation routines. Which I considered but decided against for now because of the implications it would have on using the debugger from within QT Creator.
Then I stumbled across DUMA which I got to compile using the MinGW32 compiler that comes with QT. However, I found that it doesn't really play well with QT because of the incredible amount of segmentation faults. I know they are how DUMA works, but I'm pretty sure I did not screw things up as much as DUMA tried to make me believe.
Another something I tried was the google-performance-tools which I sadly cannot get to compile with the MinGW32 version supplied with QT, even if I add in the required dependencies.
All of this brings me to my question: Is there any working solution out there for QT Creator developers that want to check their programs for memory leaks?
Thanks,
Personally, I find the valgrind / memcheck / callgrind / kcachegrind combo to be too powerful to pass up for memory leaks and performance analysis. Being free (gratis) is especially nice since I cannot personally afford the cost of some of these other professional tools.
I've been working on a cross platform Qt application for a couple years now. As I develop, I bounce back and forth between windows and linux when I want to test features and functionality. However, I always do my performance analysis in linux. The memory leaks and performance concerns that I find are always ones that are injected by my team of developers on top of Qt. So when these problems are fixed, regardless of being identified in Linux, the benefits will carry over into windows.
I suggest you investigate setting up a pure linux build environment that you can test in. Perhaps the easiest solution for you would be to create a virtual machine to do your performance analysis on. My preferred combination is VirtualBox and Ubuntu.
May be you can use these libs for windows
Hooks for Malloc [gnu.org]
Backtrace [gnu.org]
valgrind [valgrind.org]

How can I automatically detect memory leaks in C++ in a portable way?

How can I automatically detect memory leaks in C++ in a portable way? I am looking for some sort of templating solution where I can just use a macro like NEW or DELETE to track creation and deletion. It has to work on Mac, Linux, and Windows.
There are a lot of tools available for that. E.g special libraries like dmalloc libraries like libfence. On Linux especially Valgrind is very useful.
so the best "bet" probably is that you get some malloc debug libraries source code and use it in all your developments.
I would suggest running valgrind on Linux and Mac OSX, and Microsoft Application Verifier on Windows. Both tools are free.
If you would like to do it in code you can keep track of allocations in a map. At program exit you simply check if the map is empty. If you use macros you can store the source line and file with the allocation record in the map. I do however believe that using a tool is simpler and better. They can help you with much more than tracking new/delete, and do not require changes to your code.
Valgrid is good - works on Linux & Mac. You might want to try Visual Leak detector for windows.

Which C++ Profiler to use?

I am supporting a C/Cpp application and have resolved few memory leak issues by creating objects. Since it is provided by the third party API, it is told that the objects gets destructed by the API itself.
I could manage to fix the issue and test my application without any issue. But i still doubt my fix for memory leak. I want to do profiling and i am in need of a Cpp tool that can tell me if there are chances of memory leaks in the code given a CPP file.
Is there a tool of that sort? Any help is greatly appreciated.
Thanks,
~Jegan
You are probably looking for a dynamic analysis tools, not a profile. For Linux, there's valgrind. For windows some good commercial solutions are Purify and Insure++.
Give valgrind a try, it's by far the best memory analysis tool I've come across. It only runs on *nix, but you haven't stated your platform.
We're using MemoryValidator from http://www.softwareverify.com/.
It provides a fully functional 30 day trial, so you can test it before buying.
Single license from 179$ (each for 10 licenses), up to 299$ (1 license). And it is really worth it's price. Enables you to find the cause for leaks within a minute.
Also check the other tools they're providing: performance validator, bug validator and so on.
Great tool once you know how to handle it :-)

Memory leak checking on Windows with QT and MinGW32

Lately I have been developing in C++ with QT Creator. All is well and I'm nearly at the point of packaging and distributing my application. But obviously before any release you better make sure you have everything right. So I'm at the testing stage, and something tells me that I have some slight memory issues. Nothing serious, but I like to obsess over these ;-).
So I decided to try some memory leak detection libraries. For starters I looked at this question. I disregarded Purify and Insure++ because of the costs. Someone else suggested to use the MSVC compiler so I could use the CRT memory validation routines. Which I considered but decided against for now because of the implications it would have on using the debugger from within QT Creator.
Then I stumbled across DUMA which I got to compile using the MinGW32 compiler that comes with QT. However, I found that it doesn't really play well with QT because of the incredible amount of segmentation faults. I know they are how DUMA works, but I'm pretty sure I did not screw things up as much as DUMA tried to make me believe.
Another something I tried was the google-performance-tools which I sadly cannot get to compile with the MinGW32 version supplied with QT, even if I add in the required dependencies.
All of this brings me to my question: Is there any working solution out there for QT Creator developers that want to check their programs for memory leaks?
Thanks,
Personally, I find the valgrind / memcheck / callgrind / kcachegrind combo to be too powerful to pass up for memory leaks and performance analysis. Being free (gratis) is especially nice since I cannot personally afford the cost of some of these other professional tools.
I've been working on a cross platform Qt application for a couple years now. As I develop, I bounce back and forth between windows and linux when I want to test features and functionality. However, I always do my performance analysis in linux. The memory leaks and performance concerns that I find are always ones that are injected by my team of developers on top of Qt. So when these problems are fixed, regardless of being identified in Linux, the benefits will carry over into windows.
I suggest you investigate setting up a pure linux build environment that you can test in. Perhaps the easiest solution for you would be to create a virtual machine to do your performance analysis on. My preferred combination is VirtualBox and Ubuntu.
May be you can use these libs for windows
Hooks for Malloc [gnu.org]
Backtrace [gnu.org]
valgrind [valgrind.org]

What the best multi-thread application debugger for C++ apps

I'm looking for a good multi-thread-aware debugger, capable of showing performance charts of application threads on Linux, don't know if such a thing exists, perhaps as a Eclipse plugin.
The idea would be to track per thread memory allocation a CPU usage as well as being able to interrupt a thread and examine its stack trace, local vars, etc.
It does not have to be an eclipse plugin or a free tool, do any of you have heard of something similar?
Qt Creator does provide information on a per-thread basis. It also has the features you would expect from any standard debugger. (Watches, breakpoints, etc.)
Although designed for compiling Qt applications, it can be used for just about any C++ project. (I have used it for compiling/editing a non-Qt app before.)
TotalView (and MemoryScape) doesn't do precisely what you're asking for in its' default presentation, but it provides the data you need. It costs money, but a better C++ debugger for Linux cannot be found.
Free trials are available, and there are a number of cool and useful videos on their support site.
If you're on linux, you've got access to one of the most powerful debugging tools in the trade - Valgrind. Read about it, especially about it's additional tools like Helgrind.
Sure, the visualisation is lacking compared to commercial tools, but you can't beat it's level of detail.