Original Post
Hi! Wow! Have I spent a lot of time scratching my head, or what? I decided to test if my application was leaking any memory. So, I did some searching and found a nice looking tool: LeakTracer. It worked in Linux, and I'm running gcc 3.4. I obviously had a lot of memory leaks, so of course the hunt began! It took me ages to narrow dome parts of it, and I was amazingly surprised when I did find out what was causing it. I actually didn't believe it: The tool was reporting errors when calling std::string's constructor. That's in the C++ standard library, there's no way someone could have slipped a (actually several) memory leak in there, without someone else noticing. (And I was highly doubtful that I might have been the first one.) So, I spent ages trying to figure it out! Eventually, I came up with this page that explains it all. std::string, (or more exactly std::allocator) is not leaking memory. What's happening is that std::allocator uses a special pool allocation scheme, and overloads the new/delete operators. (Actually I'm not entirely certain that's how it's done, but something like that, anyway.) The memory leak detection tool didn't realize this, and reported leaked memory. The solution I found was to use another, much better tool: Valgrind, which actually handles this stuff correctly. It realizes that std::allocator is not leaking memory. It also has a much nicer interface than any of the other tools I've used. Well, this post is just a heads up to anyone looking for a memory leak detection tool. I recommend Valgrind to save you from pulling out your hair in endless frustration. Cheers, and hope this helps someone! Edit: I should probably have mentioned that Valgrind is currently only supported on Linux (2.4 kernels and up, with glibc 2.2 and up) and FreeBSD. [Edited by - James Trotter on March 27, 2005 8:46:17 AM]