Memory Checker Tools For C++? 398
An anonymous reader writes "These newfangled memory-managed languages like Java and C# leave an old C++ dev like me feeling like I am missing the love. Are there any good C++ tools out there that do really good memory validation and heap checking? I have used BoundsChecker but I was looking for something a little faster. For my problem I happen to need something that will work on Windows XP 64. It's a legacy app so I can't just use Boosts' uber nifty shared_ptr. Thanks for any ideas."
two points (Score:4, Interesting)
First of all, shared_ptr is going into the standard library as part of TR1. Does anyone know when common development environments, i.e. GCC and MSVC, will start including TR1?
Second, I believe that there are a number of garbage collectors available as libraries for C++. I've heard boehm's garbage collector mentioned numerous times. My question is, are any of these libraries any good? Are they really practical to use in real world applications? Do you have to modify all of your types to use it, or can primitive and stl types work with it?
Fluid Studio's Memory Manager (MMGR) (Score:5, Interesting)
Boost? Ugh (Score:3, Interesting)
Re:Boost? Ugh (Score:5, Interesting)
Getting it to compile was a bit of a nightmare too. It has its own native compilation management tool that you have to download as well. What the hell is wrong with using make like everybody else? It also uses a very complex template hierarchy that produces terrifying error messages.
I'm sure that once you become an expert, BGL is really powerful and efficient, but I found the learning curve too steep. I just want to get in and build a working prototype quickly so I can see what I'm doing, not spend hours wading through manuals and examples to build the simplest program.
I'll be with the parent post and get modded a troll by boost developers.
Peter
eFence (Score:4, Interesting)
Unfortunately, what you prolly want is valgrind or purify.
Re:Most tools I've tried are useless (Score:3, Interesting)
Re:Fluid Studio's Memory Manager (MMGR) (Score:1, Interesting)
Re:Most tools I've tried are useless (Score:2, Interesting)
Agreed. Most skilled coders I know don't write code that has memory leaks. Even simple things like making sure your source code looks symetrical, matching allocations with deallocations (of whatever flavor), etc., is usually enough to avoid any problems with memory. It's easy (easy!) to write loops that never overflow, though you wouldn't know it with the amazing number of overflow exploits that have been found over the years.
However, it gets nasty when dealing with legacy code that is already leaking, or when you unfortunately get partnered with somebody that doesn't know how to practice safe coding techniques. In those cases, I guess these sorts of tools are useful.
Visual Leak Detector (Score:3, Interesting)
In any case, Visual Leak Detector is a free memory checking tool. It's only for Windows / Visual Studio, but if you are using that, VLD is awesome: http://www.codeproject.com/tools/visualleakdetect
It's super easy to set up, just #include "vld.h" somewhere in your program, and then run the debug mode. No need to rebuild everything in instrumented mode, and no false results (at least I haven't got any). Real memory leaks will be reported in the output window of the IDE.
Re:Valgrind (Score:3, Interesting)
http://www.idiom.com/~zilla/Computer/javaCbenchma
Of course you can create your own "dynamic" allocator in C++... but that doesn't make allocation/deallocation automatically faster.
Maybe you should search for some numbers to support your argument... just by stating "I don't buy it, C++ is still faster" it doesn't become reality.
Re:Valgrind (Score:3, Interesting)
Speed isn't everything. If your server application is network or database bound then stability and API richness is considerably more important than speed of execution. After all, does it matter that your app completes 2ms faster if it has to wait 500ms for the database to return? Or that it crashes more frequently? Or that you can't port it to new hardware because half the libs you use don't compile? Since Java offers dozens of cross-platform APIs (network communication, web services, XML parsing, crypto, database, RMI etc. ) and runs (without recompiling) on dozens of platforms AND is generally harder to bring down than C++ I would favour where speed and UI are not factors.
Even where a UI is present, Java has it's uses. Eclipse demonstrates that Java can produce excellent cross-platform desktop applications. I think Java's weakness is definitely its UI though. While Swing is a very rich API, it's also hard to write native look and feel apps. The file picker dialog is one place where Java apps always suck. Poor look & feel is probably why Eclipse chose to use SWT instead.
C# (.NET) obviously doesn't benefit from robust cross-platform support but it does inherit most of the same advantages of Java. It also has excellent UI support (and better layout tools) on Windows meaning that you can accomplish quite a lot in C# without dropping to C++.
Re:Purify (Score:3, Interesting)
Re:Boost? Ugh (Score:2, Interesting)
And they weren't helpful errors, mind you. Due to some template magic, something had broken, and instead of telling me what really happened, Boost (at some point during compilation) replaced a template with an actual class called boost::error_property_map_not_found. What followed was a slew of errors saying that boost::error_property_map_not_found did not support the + operation, or the - operation, or...you get the idea.
The library seems plenty powerful. I think it would benefit from more examples, though. LOTS more examples.
Re:Boost? Ugh (Score:2, Interesting)
It's not primarily meant for production use, rather it is a _testbed_ for future improvements to the C++ standard library. Pretty much a place where they intentionally test the bounds of the C++ language and check what kind of features would make sense in the standard.
Re:two points (Score:4, Interesting)
Re:two points (Score:4, Interesting)
It's a pity there's no way to ensure compatibility between boost::shared_ptr and std::tr1::shared_ptr , nor a really attractive non-preprocessor-reliant mechanism to switch between them (since typedefs in C++ do not work on incomplete template types).
valgrind (Score:3, Interesting)
Re:um (Score:2, Interesting)
Other runtime commercial tools - BoundsChecker, Purify, etc
Re:Boost? Ugh (Score:3, Interesting)
I was on the C++ committee at the time that templates were accepted -- my memory is that the syntax that was accepted is identical to what Bjarne originally proposed, because there were no obvious flaws in the proposal.
It is true that if templates were being added today, I would expect the syntax to be rather different, but only because we had no idea that when we added templates we were adding a Turing-complete compile-time language. Had we known that, I am pretty sure that we would have also introduced a syntax that makes it about a million times more pleasant to actually use that feature.
Re:Boost? Ugh (Score:3, Interesting)
Re:Roll Your Own (Score:3, Interesting)
I still remember the first time I tested it; I allocated some memory, then dumped the heap. I saw the block I had allocated, but there was another 2K block allocated that I hadn't! Fortunately I was dumping the contents along with the size, and I quickly figured out that printf() was calling my allocator too! (I had written replacements for [mc]alloc() and free(), and used the same names so I could instrument existing code w/o having to rewrite it.)
Re:Valgrind (Score:3, Interesting)
However I think Java is just fine in most regular roles assuming performance is no issue. I've never thought to myself that Azureus is any slower because its implemented in Java. I've even written a poker hand calculator that runs through hundreds of thousands of hands in a second. I think performance is an overrated concern for most apps.
Even so, I still find it easier to knock together a GUI app much easier using C++ than I would in Java. Swing layout editors just stink. I don't understand what the big hangup is with Swing and visual editing since DevStudio 2005 makes it look easy with .NET 2.0.
Recent real world example (Score:2, Interesting)
My code worked fine in Windows - mostly, but still a bug remained that I suspected was in the renderer but couldn't prove it.
The code ran fine on FreeBSD without any problems but I couldn't run it for more that a few minutes on Linux without it crashing hard but at random times.
I ran my Windows code through BoundsChecker (which I've used for years and have found to be very effective). Came up fairly clean, I fixed a few things but I could not find the bug.
I wasn't familiar with any Linux debug tools but eventually tried Valgrind. It found the problem after the first run, and BoundsChecker didn't even give me a peep. I found I was stepping just outside a graphics buffer at certain times and writing in memory I didn't own. It was enlightening the bug was in the same code for FreeBSD/Linux/Windows but displayed completely different behavior.
Anyway, I have respect for Valgrind. Now, if it was only available for FreeBSD.
Re:Don't allocate or free = no leaks = need no too (Score:3, Interesting)
I don't mind if an implementation of STL has some hidden/private/singleton allocation pools behind the scenes to speed things up. What I find really friggin' annoying is that they never track those allocations and offer any form of "reset" function that you can call before exiting your app, so that the simplest global malloc-counting methods can audit for leaks. You shouldn't need an "SGI-STL-aware-and-compatible leak detector," you should only need a malloc leak detector.
Re:Roll your own! (Score:1, Interesting)
Rolling your own is much easier than it sounds. Reference counting can be done by mere mortals.
I would say that using a self-programmed memory checker to keep track of memory leaks and then fix them in your code is better than just tossing the mis-allocated memory manually. This way the memory checker can be tossed in the release version, leaving lean and mean c++ code - which is why you code in c++ anyway.
Also, most serious memory leaks are the result of conceptual confusion within code and not a simply a matter of failing to write a delete for each new. In a garbage collected language, the same confusion leads to memory hogging, stuck-loops and data-corruption. Thus c++'s lack of automatic memory management mean may seem like a terrible problem but isn't that much worse than other languages because no language can save you from your own stupidity.
Ta...