Slashdot Mirror


Memory Checker Tools For C++?

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

93 of 398 comments (clear)

  1. um by Anonymous Coward · · Score: 5, Informative

    boost::shared_ptr is not a memory checker, it's a reference-counted smart pointer, and works fine in legacy apps (such as compiled under VC++ 6).

    1. Re:um by RegularFry · · Score: 2, Informative

      It's an alternative to needing a memory checker, though, so it would provide an valid solution to the problem for a new application.

      --
      Reality is the ultimate Rorschach.
    2. Re:um by Shadowlion · · Score: 3, Insightful

      > and works fine in legacy apps

      Regarding legacy applications, I think the point was that he can't go back through the app and rewrite everything to use smart_ptr.

    3. Re:um by Evanisincontrol · · Score: 3, Insightful

      I hope that was supposed to be sarcastic, otherwise you are the worst developer I've ever heard of. Rewriting an entire legacy application just to use shared pointers is downright stupid. He might as well just redesign the entire software and build it from the ground up... but then you're not "maintaining" anymore. You've completely redefined your job description.

    4. Re:um by bhsurfer · · Score: 4, Insightful
      The first thing I was told by my boss when I got hired was "You're going to look at this app and want to rewrite it from scratch. Don't do it, that's not what we want you for." Software doesn't need to be pretty, you just make improvements as you can and leave the ugly but solid code alone until necessary. It's an extremely rare situation to have the luxury of a complete redesign/rewrite.

      I guess that's a long way of saying "I agree completely with what you just said."

      --
      Those are my principles, and if you don't like them... well, I have others.
      Groucho Marx
    5. Re:um by Anonymous Coward · · Score: 2, Interesting

      Application Verifier from MSFT is the best free tool you can get (just make sure you have good symbols, as with all run-time checkers!) - http://www.microsoft.com/technet/prodtechnol/windo ws/appcompatibility/appverifier.mspx

      Other runtime commercial tools - BoundsChecker, Purify, etc

    6. Re:um by trolltalk.com · · Score: 3, Informative

      "leave the ugly but solid code alone until necessary."

      If its ugly its not solid. Ugly code is hard to understand at first glance, and its easy to introduce an error. Or do you consider code that's easy to make a mistake with as actually being "maintainable"?

      If its ugly, there's probably a non-ugly way to do the same thing that's better, more efficient, AND more maintainable.

      Remember, 90% of the investment in code is in the ongoing maintenance. Having to "relearn" all the "cute little hacks" that make that ugly POS code work, every time you have to change something, is a waste of resources. That ugly code is usually a monument to the "there's not enough time to do it right, but there's always enough time to do it over ... and over ... and over" and "ship it now - fix it later."

      I've written enough ugly code to know that if its ugly, I'm not approaching the problem properly.

    7. Re:um by bhsurfer · · Score: 3, Insightful
      I meant "ugly" in the sense of "Not the way I would have done it" rather than in a "Holy shit, what a freakin' mess! This guy should be bagging groceries, not writing software!" kind of way. I certainly do not think that clever tricks and mounds of complex spaghetti code that were designed by avalanche is maintainable, believe me.

      I also have (unfortunately) written enough ugly stuff that when I go back later I say "I can't believe I actually did something that stupid."

      You live, ideally you learn, and when you look at code you wrote 5 years ago you likely slap your forehead in embarrassment - that's how you know you're getting better. That, and when your coworkers aren't trying to slash their wrists when they get handed something you wrote...

      --
      Those are my principles, and if you don't like them... well, I have others.
      Groucho Marx
    8. Re:um by joto · · Score: 4, Insightful

      If its ugly its not solid. Ugly code is hard to understand at first glance, and its easy to introduce an error. Or do you consider code that's easy to make a mistake with as actually being "maintainable"?

      You are confusing two aspects here. Ugliness does limit maintainability. But it does not limit "solidness". "Solidness" would mean that the code actually works, and has a proven track record, such as being used in production for over 20 years. Code that has been in production for over 20 years is usually both solid and ugly.

      That ugly code is usually a monument to the "there's not enough time to do it right, but there's always enough time to do it over ... and over ... and over" and "ship it now - fix it later."

      Or it could be a monument over "the world is a complex place, and if you change anything here, and it causes the program to fail in some weird special case, your company is going to loose umpteen zillion dollars". While the reality is probably somewhere in between, rewrites should still be avoided like the plague. However, if you really have taken the time to understand what some nasty bit of code does, there's nothing wrong about cleaning it up. But most of the time, the ugly code is there for a reason.

    9. Re:um by Anrego · · Score: 2, Informative

      Ugly code is really unavoidable. It's going to happen. While you should always try to write good, clean, maintainable code, circumstances can conspire against you. Project requirements can change half way through development with a timeline that doesn`t allow for going back and changing code properly. And of course there are always people who have ample time to write good code, but just don`t know how/don`t care.

      After having worked on what I could only describe as some of the ugliest code on the face of the earth (written by people who previously made a living writing throw away code for PhD types (the kind of code that you use to find an answer, then never use again)) I would say the _real_ problem that makes one want to just go hang themselves in the server room with some cat5 is lack of documentation.

      Nothing compounds poorly written code as much as poor (or non-existent over here) documentation. If you _have_ to hack code to make something work in a hurry... COMMENT THE CRAP OUT OF IT. Put a huge comment block, with asterisks and exclamation points and a link to the page in your projects documentation manager describing why you did it, what should be done down the road when time allows, and an apology note to the next guy who has to work on the code.

  2. two points by sentientbrendan · · Score: 3, Interesting

    This isn't really an answer to your question, but it's on topic and there's some questions I wanted to get answered myself.

    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?

    1. Re:two points by Anonymous Coward · · Score: 5, Informative

      Boehm's garbage collector is used in Inkscape -- and they did gradually introduce its use, so you can start using it for some things and gradually extend the usage.

      Boudewijn

    2. Re:two points by Anonymous Coward · · Score: 4, Informative

      gcc 4.2 includes a good part of tr1 as was released (late) a few weeks ago.

      shared_ptr is a blessing and a curse. It saves you from manually destructing objects held in a collecton (good) but too many developers use it for lifetime management (bad).

    3. Re:two points by shutdown+-p+now · · Score: 3, Interesting

      too many developers use it for lifetime management (bad).
      Why is it bad? Isn't the whole point of shared_ptr to automatically manage the lifetime of a shared resource?
    4. Re:two points by master_p · · Score: 2, Informative

      Boehm's gc is very very good...on par with Java's collector and others. The source needs cleaning up, and some configuration bugs to be ironed out, but it is very good.

      In order to use it, you just have to include 'gc.h' in your files, which replaces 'new' with 'gcnew' using macros. Alternatively, you can bypass the standard library and provide a replacement for 'malloc' and 'free', but I did not manage to do that (due to the configuration bugs mentioned above).

    5. Re:two points by Craig+Ringer · · Score: 3, Interesting

      gcc has included most of tr1, especially , since at least 4.1. I think it was in 4.0 as well.

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

    6. Re:two points by d00ber · · Score: 4, Informative

      Also, shared_ptr has been promoted to the draft standard C++-0x so you can use std::shared_ptr.

      You'll be able to use C++-0x in gcc-4.3 with a switch.

      I also heard that std::auto_ptr is being deprecated (not removed) I guess in favor of rvalue references.

      Finally, there is a motion to include garbage collection in the C++ language. This is sponsored by none other than Hans Boehm among others.

      I realize this doesn't help immediately.

    7. Re:two points by julesh · · Score: 2, Informative

      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?

      I've deployed Boehm GC in real world applications before now, and consider the quality of the collector pretty good. It isn't a real-time collector, but on a reasonably low-end computer (350MHz PII) running applications with average memory requirements (total ~50MB in objects of around 100 bytes - 2K in size) delay times were minimal (around 10ms, which I arranged to occur after event processing when it was found that no new event was waiting, hence was rarely detected by the user). You can use any type you want with it -- there are at least three different approaches:

      * Make your classes inherit from a 'gc' base class
      * Use a placement new operator (e.g. "new (GC) MyObject;" or "new (PointerFreeGC) char[16384];")
      * Use the provided 'malloc' replacement library to replace all dynamic memory allocations with a GC allocation.

      I chose to go the second route, because it gave me the most control. I had to spend a little time debugging problems caused by forgetting to include the (GC), but certainly less than I would have spent debugging undeleted objects had I not used the collector.

    8. Re:two points by trolltalk.com · · Score: 2, Informative

      "Finally, there is a motion to include garbage collection in the C++ language. "

      Call me old-fashioned, but I hope that's one that they will throw in the garbage. Call it something else if you're going to have garbage collection as an integral part. As a set of libraries, or as a compiler-time switch, fine - but not as part of the core. That's not C++, that's D.

    9. Re:two points by stonecypher · · Score: 2, Informative

      GCC got TR1 support two weeks ago. MSVS got it two years ago. Look in stdext:: in MSVS2k5.

      --
      StoneCypher is Full of BS
    10. Re:two points by akeru · · Score: 2, Informative

      You can also use the Boehm garbage collector as a leak checker. Rather than collecting garbage, it reports on (and, I *think*, frees) memory that was 'forgotten' without being properly released.

      --

      Let's hope that there's intelligent life somewhere out in space 'Cause there's bugger-all down here on Earth.

    11. Re:two points by d00ber · · Score: 3, Informative

      My understanding is that it will be opt-in. I think manual management will be the default.

      They don't want to remove the old ways (including malloc/free). And they don't want to penalize people who don't use garbage collection. In other words, if you don't want to use garbage collection you won't be paying for it in code size/runtime.

      But for them that do... It might be nice to offer it.

    12. Re:two points by Anonymous+Brave+Guy · · Score: 4, Insightful

      It's not automatically bad, but using semi-automated memory management like this tends to reduce the emphasis on constructing things only when they're needed and destroying them immediately when you're done with them. This concern, known as "Java bloat syndrome" in honour of the language that first popularised it, can lead to major performance problems in applications that manipulate a lot of data, and is a favourite mistake made by the cult of "hardware is cheap, so optimisation doesn't matter".

      The thing is, this sort of care-free programming philosophy is natural in languages like Java, so languages like Java have had to learn from their early mistakes and adapt. There have been dramatic improvements in GC technology since those early days, and today there isn't the same degree of performance penalty associated with relying on GC to clear everything up.

      However, this sort of behind-the-scenes magic isn't really the "C++ way". You can do it, but tools like shared_ptr don't have the same level of sophistication as full-blown GC. Using them requires some care from programmers, and as the grandparent post said, this can lead to problems if the programmers come to rely on them more than they ought.

      FWIW, I'm not sure I'd have described things in quite such black-and-white terms as the GP, but I can see the underlying point and I think it's a valid one.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    13. Re:two points by shutdown+-p+now · · Score: 3, Insightful

      In other words, programmers shouldn't use shared_ptr as if it were a replacement for GC. When it is worded thus, I can fully agree with that (and indeed, anyone who understands how reference counting works, will agree as well). The nice thing about shared_ptr is that, unlike GC, it is still fully deterministic, and so it properly preserves the "C++ spirit".

    14. Re:two points by TheRaven64 · · Score: 2, Insightful

      Most real projects use a build system that invokes the compiler once for each file, and then the linker once at the end. While GCC may be run hundreds of times, each invocation only lasts for a few seconds. If your code is taking longer to compile, then you should possible consider structuring it better.

      --
      I am TheRaven on Soylent News
  3. I've used... by grusin · · Score: 2, Funny

    Some time ago i was using valgrind, but afaik it does not run under windows. I think that MS Dev has some memory leak detection built in, but it is far behind valgrind. Besides, who codes stable stuff for windows? :)

    1. Re:I've used... by TapeCutter · · Score: 4, Informative

      If you use MS compilers the memory debug stuff is in crtdbg.h, IIRC it has been there in one form or another since V1.5 but for some reason seems too obscure for the average Windows programmer to find. ;)

      It is a very handy feature for finding leaks, buffer overflows, ect. The only other product I've used to find memory problems on recent incarnations of Windows is Purify. The MS solution is infinitely simpler because it's built into the environment and it's narrowly focused on memory problems. To state the obvious and in fairness to Purify, MSDev is infuriatingly fussy when it comes to building debug modules for IBM's Purify.

      --
      And did you exchange a walk on part in the war for a lead role in a cage? - Pink Floyd.
    2. Re:I've used... by inviolet · · Score: 5, Funny

      Memory checkers? GARBAGE COLLECTORS?! Have you no HONOR?!

      We are KLINGON. We need no checkers. We need no garbage collectors. We need none of these weak HUMAN facilities. We write our CODE from the COMMAND LINE:

      > COPY CON DECAPITATOR.EXE
      [Alt+077] [ALT+090] . . .

      --
      FATMOUSE + YOU = FATMOUSE
  4. Fluid Studio's Memory Manager (MMGR) by Rezonant · · Score: 5, Interesting

    Yep, Paul Nettle's little memory manager rocks. It WILL find leaks in your program. http://www.paulnettle.com/ (Yes, you have to navigate through a horrible flash site to get it, but it's worth it).

    1. Re:Fluid Studio's Memory Manager (MMGR) by Rezonant · · Score: 3, Informative

      It's true that MMGR is single thread only, and if your program allocates and frees memory on multiple threads you should choose another alternative. However, many many programs only allocate and free memory on one thread, and for those MMGR is simple to use and is pretty damn good at finding leaks, overruns and similar problems.

  5. valgrind, libgc by Anonymous Coward · · Score: 2, Informative

    Use valgrind. It's for Linux only, but what it does is invaluable for most of the tasks. I don't know of any other tool of such help.

    Note this is not 'memory management tool', but one to help you find and clean up the memory leaks. There is no way to do proper garbage collection using the STL's allocators, though there is a 'gc' library http://www.hpl.hp.com/personal/Hans_Boehm/gc/ which tends to do the job. Haven't used it, though projects like Mono http://www.mono-project.org/ use it extensively.

  6. Purify by Anonymous Coward · · Score: 2, Informative

    Purify works quite well. It is big and slow, and doens't play nicely with ACE_Tao stuff sometimes (tao do cleaver things that scare s purify sometimes)

    1. Re:Purify by 0123456 · · Score: 2, Interesting

      Yeah, I haven't used Purify in a few years, but when I tried it out it seemed very effective and found some bugs that would have been hard to track down otherwise. We didn't use it in the end because, at least at that time, it didn't like us dynamically generating machine code... otherwise it was better than anything else we tried; for normal C and C++ code it should work well.

  7. STLPort by kazade84 · · Score: 4, Informative

    I know this isn't exactly what the article is looking for. But, if you are using the STL (which you SHOULD be!) you may be interested to know that the STLPort STL implementation includes a debug mode which contains loads of error checking to make sure you aren't misusing STL.

    1. Re:STLPort by Chris_Jefferson · · Score: 4, Informative

      Actually, yes they do. In g++ use " -D_GLIBCXX_DEBUG ", in VC++ enable debugging. You'll get all these errors and more. I don't understand why everyone seems to know this part of stlport and don't realise other librarys have it as well.

      --
      Combination - fun iPhone puzzling
  8. Most tools I've tried are useless by DrXym · · Score: 3, Insightful
    I've played with Boundschecker, Purify (& Quantify) and Fortify. My experience of these tools is that they either take a painfully long time to run, throw up too many spurious warnings or crash outright after eating all available memory / disk space.

    They might be useful for small apps but if you have a massive app they are almost more trouble than they are worth.

    It's hard to say what you can do except foster safe coding practice and highlight the common pitfalls such as memory leaks, buffer overflows etc. Many compilers can help detect heap / memory overruns because the debug libs put guard bytes on the stack & heap that trigger exceptions when something bad happens. There are also 3rd party libs such as Boehm which help with memory leeak / garbage collection issues and dump stats. I'd say using STL & Boost is also a very good way of minimizing errors too simply because doing so avoids having to write your own implementations of arrays, strings etc. which are bound to be less stable.

    1. Re:Most tools I've tried are useless by cerberusss · · Score: 2, Interesting

      They might be useful for small apps but if you have a massive app they are almost more trouble than they are worth.
      I'm of the opinion that the larger the application, the harder it is to write. So my philosophy is to chop programs up in different processes. The fact that you're stating that the memory checkers are only useful for small apps, might be a sign that your app is too big.
      --
      8 of 13 people found this answer helpful. Did you?
    2. Re:Most tools I've tried are useless by gladish · · Score: 2, Insightful

      This is a result of the way that most people are developing code. They build some huge app and then finally realize that it doesn't work because it's riddled with defects and memory leaks. What should have been taking place is the creation of small units tests which were then run under some runtime analyzer like pruify. With that said, I've used purify, insure++, and valgrind. I found insure++ to be the best. I will admint that the code runs much more slowly but I was amazed at the stuff that it found. I've never used the windows version, but you can get it for windows with dev studio integration.

    3. Re:Most tools I've tried are useless by peterpi · · Score: 2, Insightful

      I love this comment, from Bjarne Stroustrup's home page (href=http://www.research.att.com/~bs/bs_faq2.html #memory-leaks)

      Q) How do I deal with memory leaks?

      A) By writing code that doesn't have any. (goes on to advocate vector & string)

      And also: C++ Is my favorite garbage collected language because it generates so little garbage (http://www.research.att.com/~bs/bs_faq.html#reall y-say-that)

      Over the past 6 months or so, I've really made an effort to better my usage of C++ (using Effective C++, Effective STL and C++ Coding Standards). With a combination of STL, references, RAII, std::string and boost::shared_ptr, all of my memory, ownership & null-pointer problems just went away. I hardly ever actually write 'new' any more. The Java model of just leaking objects and hoping they'll get collected sooner or later seems horrible.

      But I'm not maintaining old code, so this is completely -1 Offtopic.

    4. Re:Most tools I've tried are useless by Rogerborg · · Score: 2, Insightful
      > However, if one manages their objects correctly, the current generation of jvms perform quite well IMHO.

      If one manages their objects correctly, C/C++ perform quite well too.

      --
      If you were blocking sigs, you wouldn't have to read this.
    5. Re:Most tools I've tried are useless by peterpi · · Score: 4, Insightful

      I didn't explain it all that well. What I mean is; I love destructors.

      A good example of what I'm talking about is a std::ifstream versus a java.io.FileInputStream. If you make an ifstream on the stack, you can be absolutely certain that when it goes out of scope, the destructor will be called and the file closed. You can be certain that it will happen, and you can also be certain when it happens; at the very point it goes out of scope.

      With a heap based FileInputStream, you have no such gaurentee. You leak it, and you just hope that the finaliser gets called soon (if at all). I've had more than one occasion where I've been leaking FileInputStreams quicker than the garbage collector cares to clean them up, and sooner or later the OS says 'no' and you get an exception. And it's very difficult to reproduce, because it's all down to the whim of the garbage collector, and you always go slower when you're looking for a bug.

      Of course the answer to this is to say "Well you should Close() your input stream beforehand". But that's just as bad as saying "You should delete your heap based objects" in C++. It's that situation of having to manually shut down objects that seems old fashioned to me.

      Maybe there's a better way these days, I've been away from Java for a couple of years now.

      (I do enjoy coding in either language though!)

  9. Boost? Ugh by Viol8 · · Score: 3, Interesting

    Talk about a sledgehammer to crack a nut. Boost strikes me as the sort of library used by people who want to show off how up to date their skills are , not people who really need to write a program to get a job done. Its bloated , has a wierd syntax that differs from the C++ norm and doesn't solve any problem that isn't already solved or could be done quite easily by standard C++ anyway. What is its point except as intellectual masturbation by its authors? No this isn't a Troll, this is a post by someone who was forced to use Boost for a year and I loath it. Yeah , mod me down , whatever...

    1. Re:Boost? Ugh by pzs · · Score: 5, Interesting

      I would be inclined to agree with this. I used the Boost Graph Library for some research code a few years ago. It's been designed to be extremely generic, which although a good thing sometimes makes it pretty difficult to just start coding something without all the bells and whistles. For operations on graphs, such as walking through, you can use their specialised functions for doing things but it takes days and days to work out how to use them and I ended up just using regular loops because they were much easier to understand.

      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

    2. Re:Boost? Ugh by kazade84 · · Score: 2, Insightful

      If you wanna "get the job done" then boost is sometimes exactly what you need. I'm not saying that you should memorize the whole library but sometimes a boost library is available for what you want to do. I couldn't live without boost::lexical_cast, boost::shared_ptr, boost::tokenizer and boost::python is genius. You *could* write all of those things yourself but honestly, why?

      If you wanna code in C++ then you'd better get used to the "weird syntax" of templates and especially the boost libraries, they ARE the basis of most of the additions to the standard library in TR1 so they will become the "C++ norm"

    3. Re:Boost? Ugh by Viol8 · · Score: 4, Insightful

      "you'd better get used to the "weird syntax" of templates and especially the boost libraries"

      I'm used to templates syntax (though I think its ugly and Stroustrup could have done a lot better) but Boost makes it worse by overloading operators and then using them in ways never intended that produce syntax that a plain C++ wouldn't even recognise, never mind understand what its doing.eg the gratiutous overload of () for matrix ops where a simple function call would have been much cleaner and easier to follow.

    4. Re:Boost? Ugh by oergiR · · Score: 2, Informative
      You are misinformed. You probably don't have any experience with Boost, nor do you get your facts right. Boost consists of different parts, and for using boost::shared_ptr you need only a couple of headers. There is even a tool that extracts the necessary bits for you.

      Its bloated , has a wierd syntax that differs from the C++ norm and doesn't solve any problem that isn't already solved or could be done quite easily by standard C++ anyway. boost::shared_ptr has been in standard library implementations for a couple of years now as std::tr1::shared_ptr. It will most likely be included in the next C++ standard. Apparently the C++ standard committee did think that shared_ptr solves problems that the old C++ standard does not.
    5. Re:Boost? Ugh by Cyberax · · Score: 2, Informative

      You don't usually need to build anything to use BGL - it's mostly header-only library. It has only two small helper files.

      And Boost.Build is muuuuuch more powerful than makefiles. You can try to use Boost.Build v2 in your own projects - it's very very useful.

    6. Re:Boost? Ugh by rabidgnat · · Score: 2, Informative

      In 1.34, Boost made a Windows install tool that makes it much more easy to use. Just run the installer, put it in the directory you want, and change the Visual Studio settings to include $BOOST_ROOT. You could easily get going in 5 minutes

      By the way, if you're not developing for Boost and aren't using one of 8 or 9 libraries, you don't *have* to run their build system. Just pointing Visual Studio or GCC to $BOOST_ROOT is all you need to do. It sounds like taking 5 minutes to read the "getting started" page would have saved you a lot of grief ;)

    7. Re:Boost? Ugh by MikeTheMan · · Score: 2, Interesting

      I pretty much had the exact same experience. I tried to use the Boost Graph Library to create a visibility graph for a motion planning problem, and while it eventually did work, it was painful to get it working. At one point, I spent 3 hours (!!) trying to get the program to just compile. Errors were awful, kinda like STL errors but on steroids. In a standard-size terminal, each error took up about 8 lines.

      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.

    8. Re:Boost? Ugh by maxwell+demon · · Score: 3, Insightful

      "I suppose you like adding vector components manually, instead of doing v1 + v2?"

      No , something like vectorAdd(v1,v2) would be a lot more readable and a damn site easier to grep for. Idiot. Then probably we should remove the operators for built-in types as well. After all, you could use functions like doubleAdd(a, b). As a bonus, you'd not get nasty surprises when mixing unsigned and signed integers. intGreater(a, n) would always give you the expected answer, even if a is negative and n is unsigned. If you'd want to compare in unsigned arithmetics, you'd use uintGreater(a, b) instead. And what dereferenePointer(p) does is self-evident, unlike *p. Also, removing all operators would greatly simplify the parser, because the only types of expressions it would have to parse would be constants, variables and function calls.

      But I just see you signed your post with "Idiot." Thus I guess I shouldn't have taken it seriously anyway. :-)
      --
      The Tao of math: The numbers you can count are not the real numbers.
    9. Re:Boost? Ugh by marnek · · Score: 2, Informative

      If I saw v1 + v2, how would I know if it's adding the numerical vectors v1 and v2 or appending v2 to the end of v1 ? What if v1 and v2 are not numerical? Or do you think people should just intuitively know that for numerical vectors the + operator means element-wise addition and otherwise it means concatenation?

    10. Re:Boost? Ugh by bzipitidoo · · Score: 2, Funny

      How about some Reverse Polish (aka postfix): v1 v2 +

      Gets rid of all those pesky parentheses, simplifies the syntax. What's not to like, unless you're a LISP fan? ;)

      For grep's sake, you could, oh, I dunno, use comments? v1 v2 +; //vectorAdd
      Or, maybe write out the function name? v1.operator+(v2);
      Possibly even specify the class name too? Not sure of the exact syntax, perhaps: Vector::v1.operator+(v2) or v1.Vector::operator+(v2). If postfix was allowed, maybe: v1 v2 Vector::operator+();

      Oh, and are you not a fan of OOP, which I thought was the whole point of C++? Otherwise shouldn't your example be v1.vectorAdd(v2) ?

      --
      Intellectual Property is a monopolistic, selfish, and defective concept. It is "tyranny over the mind of man"
    11. Re:Boost? Ugh by N7DR · · Score: 2, Interesting
      I'm used to templates syntax (though I think its ugly and Stroustrup could have done a lot better)

      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.

    12. Re:Boost? Ugh by Viol8 · · Score: 2, Interesting

      Are you seriously using those examples as an argument FOR readability?? I hope you're just being sarcastic.

    13. Re:Boost? Ugh by swillden · · Score: 2, Informative

      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.

      Huh? Where did you get that idea? The Boost libraries most definitely are intended for production use, in a wide variety of environments. From the home page:

      Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use.

      They do aim to define and refine libraries so that they may eventually be appropriate for standardization, but the *primary* purpose of the Boost libraries is to provide tools that working programmers need, to get their production work done.

      --
      Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.
    14. Re:Boost? Ugh by pzs · · Score: 2, Insightful

      Amen, brother.

      The only thing I can add to this is that an error message that only takes up 8 lines is a cissy error coming from BGL. I had errors that were multiple screenfuls. It seems somehow wrong when a tiny type error that can be fixed with maybe 3 or 4 well placed characters can be so verbose. I guess that's C++ for you.

      Peter

    15. Re:Boost? Ugh by stonecypher · · Score: 2, Informative

      By definition it is both. It is important to remember that no candidate library may be admitted to C++ if it isn't production ready. The idea that Boost is not meant for production use suggests that you too might do well with a bit of catching up on the ol' intents-and-philosophies set.

      --
      StoneCypher is Full of BS
    16. Re:Boost? Ugh by NoOneInParticular · · Score: 2, Insightful
      The effect in Java is like every pointer (or object reference or whatever you want to call them) were shared_ptr (except better).

      Nope. garbage collection solves one problem, memory management, but does not solve the more general issue of resource management. Incorporating a few file handles, database connections or what you have into Objects in java leads immediately to manual resource management issues. You cannot reflect a couple of resources into an object and have deterministic release behaviour unless you explicitly code for it. Shared pointers (reference counting) does cater for this, albeit at a performance cost. RAII is impossible in Java, yet commonplace in C++, with or without reference counting. They're just different, each with tradeoffs of their own, mkay?

    17. Re:Boost? Ugh by John+Betonschaar · · Score: 2, Insightful

      I was not talking about Boost, that's a completely different subject (on which I mostly disagree with you as well but lets forget about that). I've read most of your replies in this topic, and that's what I'm basing my judgement on. You cannot seriously believe that things like operator overloading, design patterns, RAII, Boost, templates and the other things you see no purpose for are all 'flavour of the month paradigms' and only useful for programmers to show off. If you do, you are not an experienced developer to me, at least not one that moved along with advancements in the field. But maybe you're just trolling by blowing your arguments completely out of proportion.

      If you really consider yourself a skilled C++ programmer you'd acknowledge that C++ provides 1001 ways to do the same thing. For some purposes using operator overloading and templates is better, for other purposes using method overloading and OO inheritance is better. Same goes for other problems C++ offers multiple solutions for. Sometimes multiple inheritance is ok, sometimes it is terrible. Heck, sometimes using 'goto' even makes sense. If you're not only consider yourself a skilled C++ programmer but also a skilled software engineer, you'd also acknowledge that code re-use and design patterns are almost always good things if applied properly, irrespective of the implementation language. If you say 'design patterns' is just new and cool terminology for clueless programmers you probably never even opened the de-facto standard work about design patterns (Gamma et. al) and browsed it a little. It's just common solutions to recurring problems, that can save you a lot of work because you don't have to re-invent them yourself. It's just design re-use on the architectural level, which is even more important than re-use on the implementation level.

      I think you should try coding up some Java, C#, D or Python some day. You'll probably be disgusted by all the 'paradigms of the month' they applied to those languages, how much re-use and design patterns are incorporated into them etc. You think it's just because the people who created the languages wanted to show off?

  10. You are looking for PageHeap by Photo_Nut · · Score: 4, Informative

    PageHeap is a debugging tool for Windows created by Microsoft. It does what you want.

    For more information look here:
    http://support.microsoft.com/kb/286470

  11. Valgrind by bms20 · · Score: 5, Informative

    Use valgrind : www.valgrind.org It is (in my opinion) the best tool available for this purpose. In fact, I develop C++ exclusively on linux first because of valgrind, then port to Win32 later. By the way, you're not missing out on anything special by not programming in Java or C#. Both of those languages are slow, and introduce their own language complexities. -bms20

    1. Re:Valgrind by HRogge · · Score: 3, Informative

      If you still think Java/C# are slow, especially in terms of memory management you might want to read this:
      http://www.ibm.com/developerworks/java/library/j-j tp09275.html

    2. Re:Valgrind by HRogge · · Score: 3, Interesting

      Another link you might like to read (just done some search on google):
      http://www.idiom.com/~zilla/Computer/javaCbenchmar k.html

      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.

    3. Re:Valgrind by DrXym · · Score: 2, Interesting
      By the way, you're not missing out on anything special by not programming in Java or C#. Both of those languages are slow, and introduce their own language complexities. -bms20

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

    4. Re:Valgrind by Idaho · · Score: 2, Informative

      Use valgrind : www.valgrind.org It is (in my opinion) the best tool available for this purpose. In fact, I develop C++ exclusively on linux first because of valgrind, then port to Win32 later.


      It's been a few years since I last programmed in C++, but Valgrind indeed really saved the day on a regular basis. Also look into (KDE-)frontends if you think looking at text output is not very convenient. Couldn't agree more with this part, it's the best tool I've seen - and it's free software, too.

      By the way, you're not missing out on anything special by not programming in Java or C#. Both of those languages are slow, and introduce their own language complexities. -bms20


      Afraid I have to disagree here though. First of all, a language is not the thing that's "slow" or "fast". It may be the case that no very efficient compilers or virtual machines exist (for a particular language). I will admit that it is hard(er) to create efficient implementations of some languages (functional languages, logic-based languages), but C# and Java are definitely not among those. Second, Java VM's used to be slow in 1997. It's 2007 now, I'd suggest you try again (and be surprised). Third, I definitely do not agree that "you're not missing out on anything special" by using C++ instead of any garbage collected language (not necessarily Java or C#). For one, you're (do I need to state the obvious here?) missing out on garbage collection! I would say garbage collection is a clear advantage in the vast majority of programming scenario's. I would even argue that it's the biggest practical advancement in programming since the introduction of the procedural paradigm - perhaps even more important than object orientation.

      Last of all, you're complaining about "language complexities" in Java/C# and (thereby) suggesting C++ is better in this regard? Hmm, I guess my sarcasm detector must be broken or something :D
      --
      Every expression is true, for a given value of 'true'
    5. Re:Valgrind by Rogerborg · · Score: 3, Informative

      Agreed. I am not a fan of Linux development environments, but there really is nothing like Valgrind available for Windows, so run as much of your code as possible through Valgrind on Linux. I'd say that it is worth the effort, even if you have no intention of supporting a Linux distribution.

      --
      If you were blocking sigs, you wouldn't have to read this.
    6. Re:Valgrind by DrXym · · Score: 2, Interesting
      I think my perspective is rounded because I'm in regular contact with C++, Java and C# every day at work. I wouldn't dream of using Java for something performance related (or I'd at least use JNI to fix the performance bottleneck) and I wouldn't dream of using it for a lightweight GUI app that I expect launch quickly.

      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.

    7. Re:Valgrind by stonecypher · · Score: 2, Informative

      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.

      "Speed isn't everything. Why, if you start with a situation where other problems are choking you, you don't even have to think about speed!" That's called tautology.

      By the by, the programming languages you just argued for are ADA, PHP and Visual Basic.NET, whose libraries are enormous in comparison to Java or C#. Now, I do a fair amount of PHP when I'm bored, so don't get up in arms when I say this. That said, I want to point something out: there are very, very few genuinely large scale services written in PHP, C#, Java, ADA, or any of those languages.

      When it comes down to it, there are two costs: programmer time and hardware time. If you're network blocked, you buy a bigger network pipe; a dedicated box with a dedicated guaranteed ten megabit unmetered line for a year costs about a week and a half of a programmer's salary.

      But, more importantly, you never, ever, EVER get network blocked. That just doesn't happen on engineered systems. The network moves faster than any software you or anyone you know will ever write. Sure, one given pipe might fill; you just upgrade the pipe. It's relatively straightforward to find gigabit, and if you know what you're doing you can go up from there; you have almost certainly never in your life seen a system that can push a gigabit of data. Even just filling static HTTP requests at that speed can bring a heavy duty, well tuned box to its knees. I happen to be friends with a tech at one of the giant shared hosting farms; his company has over 200,000 customers, and it takes them a Quad Athlon to service the average 100 megabit pipe filled with average wordpress blogs.

      Now, I'm not saying the richness of the API doesn't matter. I'm just saying that positing a system based on filling the network is a little like designing the heater in the car in case the sun goes out. If you have enough users to fill a pipe, you can afford the next pipe up, or you need to be less retarded in setting your prices. The pipe has nothing to do with your selection of language.

      And, frankly, the idea that you have to step away from real languages to get a real API is silly. Or, haven't you ever actually looked through the standard libraries of languages like C++ and Erlang?

      After all, does it matter that your app completes 2ms faster if it has to wait 500ms for the database to return?

      If your database is taking a half second to return, you've got incredibly serious problems. And yes, in the real world, a 2ms lag matters because of the queueing problem. Have a look at one of those graphs where a modern stage server like Lightstreamer or YAWS compares itself to Apache. Apache tends to drop off logarithmically. Every time you lag 2ms, that's two more MS of customers you have to deal with during the next query.

      This is a limit flow problem, and most people get limit flow problems if you use a toilet as an analogy. Your webserver is similar to a toilet that doesn't have a stopgap. That means the bowl is always slowly filling with usage (ie, not water - say this is a train station) and you have to flush it every so often to keep things clean. The train station was well designed 50 years ago, but as population has gone up, the facilities are feeling the strain.

      Now, there's a point at which you say "does it really matter if the toilet takes three minutes to flush, if it's 20 minutes between trains?" Well, actually, yes, because that means each toilet can only service six and two thirds people per train arrival. At home-bound rush hour, you're going to get an enormous line of people outside the bathroom that keeps getting longer and longer.

      The problem with Apache is that the longer the line is, the slower the toilet flushes. See the issue now

      --
      StoneCypher is Full of BS
  12. eFence by cannonfodda · · Score: 4, Interesting

    I haven't used it for a while, but I used to use Bruce Peren's efence for bits of malloc debugging, it hasn't been actively developed for ages but it's pretty light weight if that's what you need. There appears to be an up to date branch DUMA which I haven't tried. As far as I remember you can use efence under WIN and DUMA claims to work......

    Unfortunately, what you prolly want is valgrind or purify.

    --
    Hmmmmmm
  13. Re:Duh! by WrongSizeGlass · · Score: 4, Funny

    Replace new/delete, malloc/free, whatever/whichever, with your own tracking version. In the end you may come out with an even better idea of memory handling for whatever you are working on at the time. God-awful simple you idiot !! You disgust me that you are so stupid !! Just replace that post with your own comment system, such as replacing God-awful with I'm, idiot with smart, disgust with inspire and stupid with curious.
  14. Visual Leak Detector by Tucano · · Score: 3, Interesting

    I have tried mainly Boundschecker and Purify, and they were usually quite slow and difficult to set up and produced lots of spurious results. Also, quite often they simply didn't work at all and refused to run certain programs. A few years ago I reduced the problem to a 10 line C++ program that would crash Boundschecker or Purify, can't remember anymore which one it was.

    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/visualleakdetecto r.asp

    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.

  15. I must agree - Purify is it. by Anonymous Coward · · Score: 3, Informative

    I used to select tools for my team. For UNIX, I selected Purify and for Windows, BoundsChecker. After a few days with BoundsSlacker, **all** and I mean every one of my team was asking for Purify for Windows licenses. 17 licenses later and our code crashes have dropped byat least 90%.

    Yes, it isn't cheap. Just do it. You'll thank me.

    The productivity increase alone will make it worthwhile for management.

    1. Re:I must agree - Purify is it. by John+Betonschaar · · Score: 2, Informative

      [i]A few years ago I also tried out Insure++ from Parasoft, which seemed to be of similar, if not slightly better, quality.[/i]

      We 'use' Insure++ as well, but unfortunately 'using it' is limited to tracking down arcane, semi-valid C++ constructs in our code that Insure++ b0rks on. Insure++ supposed to be a pretty advanced tool, but it is not actively maintained anymore and it's full of limitations that make it almost unusable for existing codebases. Especially stuff with templates, stuff using classes from external namespaces and dodgy C++ constructs that compile without warnings on every compiler I know will make Insure++ instrumentation fail. Also, linking the instrumented source files together fails on 1 out of every 10 object files, especially if they also link against third-party static libs (.a on linux). Sometimes you can fix this, but most of the time it is completely unclear why the thing fails. This effectively rendere the whole tool useless as you cannot be sure there will be no problems left in the non-instrumented parts of your codebase. And in those cases instrumentation *does* work, the output you'll get when running the instrumented binaries is sometimes really unclear, confusing or downright nonsense.

      Last but not least, Parasoft support is awful. We've been told some of our linkage problems, for which we filed bug reports, would be fixed in a later release. No new version was released for 1.5 years, and on multiple inquiries about this we did not even get a reply. Then, all of a sudden Parasoft released a new version a while ago, which we found out about 4 months later as they did not bother to notify us about it...

  16. A second vote for Valgrind by RatCommander · · Score: 3, Insightful

    Valgrind's default memcheck tool is an excellent way of finding memory errors - ranging from extremely subtle to obvious. In addition, Valgrind can be used as a code profiler, cache simulator and many other things. It really is an excellent tool - I recommend it to anybody writing C++.

    --
    "It is better to die for an idea that will live than to live for an idea that will die" - Steve Biko
  17. Detailed article on memory usage by Tronster · · Score: 3, Informative

    Last week on Gamasutra was a good article on memory leak detection, and how to role your own tool:

    "Monitoring Your PC's Memory Usage For Game Development"

    While the title says it's for game development, I found that the meat of the article applies to any windows developer.

  18. Most people can't understand Purify's output by Anonymous Coward · · Score: 5, Insightful
    Most people can't understand Purify's output, and I've actually ran across coders who actually believe their code can't be as bad a Purify says it is.

    For example, this code has serious issues:

    extern string method_that_returns_string_object();
    char *ptr;
          .
          .
          .
    ptr = method_that_returns_string_object();
          .
          . . That actually will compile, and seem to "work". But it's horribly wrong, and Purify will find the problems.

    And FWIW, I've used Purify on massive apps, and found huge problems that the developers didn't even know were there. On one project, they couldn't explain why their "perfect" app kept crashing, either. Worse for them, I had been hired as a consultant to fix their problems that they couldn't seem believe existed (HINT: your boss hired someone from the outside...), and after watching the team flail and spend literally almost a man-year trying to find one memory bug, I finally had enough of "advice giving" being ignored and got on their system, linked their app under Purify, ran it, and found the bug - a double delete of an object from two different threads. It all took me about fifteen minutes. I did that in front of their management. I made my point.

    Purify (and like tools) are a great help. Not using them is like trying to build a house without power tools. Yeah, it can be done. But what would you think if hired a builder to make your house and his team showed up carrying hand saws? Oh, and you are paying that team to hand-saw all the lumber...

    What would you think of that builder?

    Yet, when a developer asks for tools like Purify, management often balks. Because 1) they're shortsighted, and 2) developers don't know how to use such tools.

    Like I said - what would you think of a construction company where the workers don't know how to use modern power tools to help their productivity?

    Well, you just put yourself in that category.

    Yes, Purify is somewhat slower than running without Purify. But it's a lot faster than most other full-memory checking methods. If you're worried about speed, link against the Win32 debug libraries - they'll at least show problems with double free() calls, access of free()'d and deleted objects, etc. And without too much performance problems.
  19. Don't allocate or free = no leaks = need no tools by seniorcoder · · Score: 5, Insightful

    We are running many high speed financial message processing applications. A crash for any reason (including a leak) would be very costly for us.

    We pre-allocate pools of objects at startup and then re-use them. No other memory is allocated or freed while the process is running. Our pools of reusable objects are monitored very carefully as an object that isn't release back to its pool when the job is done is akin to a memory leak. Use of sentries to automatically release objects back to the pools when they fall out of scope is mandatory.

    So my answer is to the problem is:
    1. Use sentries (or some other mechanism) to guarantee memory is released.
    2. Don't allocate except at startup.
    3. No need for elaborate tools due to the above.

    I'm sure that not all applications data usage would fit into this model, but it is surprising how many can.

    We have seen some leaks in our applications. These were tracked down to STL internally leaking. They weren't generally very large and therefore we continue to live with them.

    On the subject of garbage collectors, some of our colleagues use Java and .NET. Both sets of colleagues have had major performance problems caused directly by the garbage collectors kicking in and consuming vast CPU power while they did their thing. The result was a failure to process messages in a timely manner in our high speed environment. The solution in both languages was to use pools of reusable objects and never cause their reference counts to drop to 0. Thus they implemented the very same mechanism that we use in C++ and avoided the garbage collectors.

    So don't think that a garbage collector is the solution. Perhaps in less demanding applications it is a potential answer.

    Lastly, I strongly dislike anything from Rational. I find them overpriced unreliable bloatware (YMMV). Purify used to be good some time ago, but those days are long gone.

    I echo what others have said above. You are a developer. You know your requirements. Build a simple tool to monitor and check your usage. For us it was managed pools of re-usable objects.

  20. Memory Validator by sdt · · Score: 3, Informative

    Have a look at memory validator. I don't know if it supports 64 bit applications, but it has a great list of features and is the only decent memory validation tool I've ever seen on Windows.

  21. Don't use pointer arithmetic? by wonkavader · · Score: 2, Funny

    - Avoid pointer arithmetic.

    But why use C++ if you're not using pointer arithmetic? If you're not doing that, go to Java. I know this sounds silly, and it opens religious issues, but (aside from legacy apps, like this one) why would I want to use C++ if I'm not doing that?

    I use C because it's small, fast, convenient and portable. I can code something tiny quickly.

    I use Java because it's an object oriented language that helps with complex app coding.

    I use C++ because I want some of that Java stuff, but I want it to bind to my memory model more realistically. ie. Pointer arithmetic.

  22. Memory Validator by Gedalia · · Score: 2, Informative

    I have had pretty results from Memory Validator from Software Verify. (
    http://www.softwareverify.com/cpp/memory/index.htm l ) It'll slow down your app but I think it does a better job keeping things close to real time then purify.

    -----------
    Fight Entropy!!! Fight Entropy!!! Figth Etnropy! !
    iFgth Etnrop!y ! giFth tErno!py ! giFt htrEno!p y! --- Well maybe
    not...

  23. Use a testing framework by Heir+Of+The+Mess · · Score: 3, Informative

    Use a testing framework like Parasoft's CPP stuff http://www.parasoft.com/jsp/products/home.jsp?prod uct=CppTest

    --
    Australian running a company that does C# / C++ / Java / SQL / Python / Mathematica
  24. valgrind by nanosquid · · Score: 2, Interesting

    Use valgrind.

  25. GCC mudflap by MORB · · Score: 2, Informative

    If you can afford to make your app compile with mingw's gcc (or cygwin's gcc but mingw should be easier), then you may be able to use mudflap, which is a memory debugging system integrated into gcc. You just need to pass -fmudflap, and gcc will instrument the program at compilation time.

    One thing is that it used not to properly instrument some really basic C++ operation in gcc 4.1 (I don't remember what exactly, something like copy construction of an object containing pointers) and was reporting spurious leaks because of that. It may have been fixed in 4.2.

    Search for "mudflap" in the following page: http://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/Optimi ze-Options.html#Optimize-Options
    As well as http://gcc.gnu.org/wiki/Mudflap%20Pointer%20Debugg ing (maybe slightly outdated)

  26. Re:Roll Your Own by Doctor+Memory · · Score: 2, Interesting

    Exactly my experience. I didn't know Electric Fence existed (and it may not have, this was back in '92-'93), so I wrote my own malloc replacement with bounds checking. It didn't eat up much more memory (I think around 64 bytes/allocation) or use a whole lot of CPU (basically, it walked the heap checking for corruption every N allocations, and N was configurable down to 1).

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

    --
    Just junk food for thought...
  27. Microsoft Application Verifier by ChriSindri · · Score: 2, Informative

    Application Verifier comes in x86, ia64 and amd64 flavors.

    This tool allows you to enable PageHeap for your process, which is heap corruption detection built into the OS heap implementation. Upon freeing a block of memory, PageHeap will break into your debugger spewing tracing that a block has been corrupted. It can also provide the call stack when the block was allocated. Newer heap validation features are available in progressively more recent OS releases.

  28. Home brew tool for memory leaks with glibc by cant_get_a_good_nick · · Score: 3, Insightful

    GLIBC allows you to create hooks for the standard mem functions (malloc/realloc/free). Remember that g++ still calls these under new/delete so it works for C++ also.

    One of our guys coded up a simple shared lib that can be loaded with LD_PRELOAD that sets simple hooks of printing memory locations for new/realloc/delete. He then wrote a perl script that kept track of these things and spit out anything that was malloc'ed and not realloc'ed or free'd.

    I can't post it, because technically it's not my code it's my company's. But his shared lib code is just 300 lines long, and shouldn't be hard to duplicate. The perl log filter is even more straighforward. Each malloc gets saved. Each free removes the malloc. Each realloc removes the old malloc and adds a new one. Anything left over is a leak.

    Override __malloc_initialize_hook with a pointer to your init_function. In your init_function, save the old functions at __malloc_hook __free_hook __memalign_hook and __realloc_hook and substitute your own. Now write your replacement functions, in it, do your logging and temporarily replce the old hooks and call the original functions, replace with your hook on the way out to get the next call. All of the hooks should be wrapped in a mutex to help re-entrancy problems.

    It's not a full memory detector, just does leaks, but it's non-intrusive, requires no recompiles, and is the best way we have to leak detect our huge server long running code.

  29. Recent real world example by 2DGamer · · Score: 2, Interesting

    I can attest to this. A few months back I was doing cross development for FreeBSD/Linux and maintaining a development version for Windows.

    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.

  30. Re:Don't allocate or free = no leaks = need no too by Speare · · Score: 2, 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.

    --
    [ .sig file not found ]
  31. Re:Those "skilled coders" must not code much by ShakaUVM · · Score: 2, Insightful

    >>If you write a lot of code, you WILL make mistakes like memory leaks. A lot of them. If you don't think so, you're living in
    >>fantasy land and you're nowhere near as good a coder as you think you are.

    Pfft.

    Actually, good coding habits will indeed work.

    We were three people coding a 100,000 line program, 0 memory leaks. C.

  32. Re:Are you thick or what? by maxwell+demon · · Score: 2, Insightful

    Why should I ever have to do that? Either I'm checking the correctness of operator+, then I'm interested in the implementation of it, and don't care at all about who might call it where for whatever reason. Or I'm checking the correctness of code which uses operator+, and then I already know where it is used (in the code I'm checking right now, and I don't care it it is called from anywhere else).

    Indeed, with generic programming, the same code may call several different implementations of operator+, depending on what type it is used on. The same goes BTW for normal named functions. And the same is also true for virtual member functions (operator or not) in an OOP context.

    I'd say if you have to find the callers of operator+ in order to check if it is implemented correctly, there's something fundamentally wrong with your code.

    --
    The Tao of math: The numbers you can count are not the real numbers.
  33. Re:Are you thick or what? by Viol8 · · Score: 2, Insightful

    Meanwhile , back in the real world...

  34. Re:rtti by MenTaLguY · · Score: 2, Informative

    All they would need is a more robust run-time type information system.

    No, not at all. The current C/C++ specifications permit compilers to transform code in ways that can interfere with a garbage collector. Fortunately compilers do not do that as often as they could, but it seems like something important that should be addressed.

    See Hans' paper Simple Garbage-Collector Safety for details.

    --

    DNA just wants to be free...
  35. GC is good, but relocation +GC is better by Anonymous Coward · · Score: 2, Insightful

    I was a developer for a "top 5" web browser for 6 years. I often worked on porting to small platforms and I also focused on how to improve performance on these platforms.

    Before I go into a rant, I must first rant about how much I hate Java. I feel that it was a great proof of concept and that they should have taken what they learned and went back and did it better. Java is a lot of great ideas implemented poorly due to lack of experience. I think they should have spent more time with the SmallTalk guys who actually almost had it right to begin with. Hell, evolving SmallTalk would have been far more intelligent than turning C++ into SmallTalk.

    Ok... here's the thing. I tried a lot of competing products. I tried memory checkers, memory allocators (and SmartHeap is the shit!), I tried memory profilers, hand instrumented memory logging, etc... what I've learned are a few things...

    Garbage collection (even reference counting) can improve performance greatly, but it had little or no impact on fragmentation. A system that I slapped together as a malloc/free new/delete override proved quite successful at drasticly improving browsing performance. What I did was that instead of deleting memory, I queued deletes and when the pool needed to be grown, I would process the deletes or I would process the deletes during idle cycles.

    This just made the program seem faster during runtime... obviously, the added overhead just made it slower.

    To explain why a web browser is one of the most rigorous tests of a memory environment... just think of the hundreds to thousands of DOM nodes/elements/etc..., script objects, images, etc... there are in sinlge page. Add that each element is typically represented by a single allocated object. Consider that images can decode to 100 megs in size (yes, it happens), most often closer to 2-3 megs for background images.
    A web browser can't use a memory system optimized for specific object sizes.
    Due to the dynamic nature of the objects, object reusability is not really an option
    Scripts can grow or shrink memory usage thousands of times per second
    Browsers typically contain 3rd party code from plugins which need to interact with the browser

    I can go on and on... a web browser is possibly the worst memory management nightmare on the planet. Often I worked with customers that were developing their own operating system. They used to tell me that my web browser must suck because making it stable on their system was a pain in the ass and often required them to either change or rewrite their entire memory management system to get good performance on embedded devices. Then I'd explain to them that up until now, their memory manager has been having a friendly snow-ball fight with a penguin, now it's running for it's life from an avalanch caused by a mean Yeti.

    So here's the deal, GC really didn't pay off for us... helped a little, but I have to say that once we started using reference counting and simple GC, the quality of the code got really poor. Just look at Symbian for an example of a product that suffers from using great memory management system that increases coding complexity 10 fold. It makes it so that you spend all your time coding for the memory manager and you run out of time to make the program itself work.

    Now on the other hand, I played with a few Java web browsers and learned something important. Java is made for phones. If it has no other purpose (and I'm convinced it doesn't), it's for embedded devices. Because of relocatability, fragmentation doesn't occur (in a good VM) and applications run much better. The GC + Relocation system is REALLY REALLY REALLY good, if I were to start writing a new web browser today, I'd find a good alternative to Java and get moving on it.

    Oh... last thing about auto-pointers, they're a blessing and a curse. For the most part, I find the best solution to be to use a proper system library like Qt instead of boost or STL. Qt seems to actu