LWN: Comments on "Valgrind 3.9.0 released" https://lwn.net/Articles/572790/ This is a special feed containing comments posted to the individual LWN article titled "Valgrind 3.9.0 released". en-us Sat, 18 Oct 2025 01:05:06 +0000 Sat, 18 Oct 2025 01:05:06 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Valgrind 3.9.0 https://lwn.net/Articles/573271/ https://lwn.net/Articles/573271/ pr1268 <p>Thank you (and everyone else above) for your replies. Quite enlightening discussion, I might add.</p> <p>Granted, my C++ &quot;toy&quot; program was merely an academic exercise; I also posted this to <a href="http://stackoverflow.com/questions/4250281/possible-memory-leak-using-c-string">stackoverflow</a> a few years ago (my question was more related to how the C++ standard library manages <tt>std::string</tt>), and the resultant discussion was nearly the same. For the record, I do not write code like that (at least avoiding calling <tt>exit(3)</tt> in a silly manner), and also I question my own usage of C++ <tt>std::string</tt> when it's not really needed.</p> <p>Thanks again, everyone!</p> Thu, 07 Nov 2013 22:39:46 +0000 Valgrind 3.9.0 https://lwn.net/Articles/573247/ https://lwn.net/Articles/573247/ rriggs <div class="FormattedComment"> Thanks for posting this. Too many C++ programmers are unaware of this issue.<br> <p> "Never exit(3) from a C++ program; either return from main() or abort(3)."<br> <p> That should be Sutter &amp; Alexandrescu's 102nd rule (or 101st, depending on how you count).<br> <p> <p> </div> Thu, 07 Nov 2013 18:10:21 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572886/ https://lwn.net/Articles/572886/ iabervon <div class="FormattedComment"> This bug is specific to simple objects (strings and arrays) and relates to the fact that the standard library allocates them oddly. If valgrind knew what the standard library were doing, or the code used an object with an interesting destructor, valgrind would give the correct classification, which is "still reachable". Valgrind is correct to warn you, but it's giving the wrong warning, which makes it hard to know what to fix.<br> </div> Tue, 05 Nov 2013 18:11:55 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572859/ https://lwn.net/Articles/572859/ hamjudo Forgot a tip. We renamed main() to realmain(), and wrote a new main() that called the realmain(). realmain() was supposed to free everything it allocated. This gave us a place to put a breakpoint after realmain() returned, but before the system libraries did their cleanup. From the debugger, we could ask Valgrind what was still allocated. Tue, 05 Nov 2013 16:13:54 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572854/ https://lwn.net/Articles/572854/ hamjudo Add a "--slow-exit" option to the program that when used bypasses the normal fast exit path, marks the time, then deallocate everything. If you have issues with consistency of in memory structures, you can add something to walk every structure before deallocation. If you suspect that something is happening in destructor, that shouldn't, use strace to make sure there is no I/O after the mark.<p> A few jobs ago, we set up cluster of systems to run Valgrind each night on the most recent version of the code. It was amazingly useful. The code was built the same way as what a customer would have gotten, but some parameters were different, so we could run the tests, and verify memory wasn't being lost. Tue, 05 Nov 2013 16:00:10 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572847/ https://lwn.net/Articles/572847/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; Could valgrind somehow find out whether those objects have destructors that could matter?</font><br> <p> Short answer: no. It would mean that Valgrind would understand your intent, and/or the intent of every library you happen to be using. <br> <p> <font class="QuotedText">&gt; it would be nice to be able to simply ignore resources you don't care</font><br> <p> The problem is you should probably care. Is this last line of output that has yet to be flushed important? That very last commit to the database? The final UDP confirmation packet? Who hasn't seen destructors abused this way a hundred times?<br> </div> Tue, 05 Nov 2013 14:42:40 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572846/ https://lwn.net/Articles/572846/ jwakely <div class="FormattedComment"> N.B. glibc doesn't have an implementation of std::string, glibc is the C library, the implementation you're referring to belongs to libstdc++, which is part of GCC.<br> </div> Tue, 05 Nov 2013 14:38:10 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572836/ https://lwn.net/Articles/572836/ mathstuf <div class="FormattedComment"> It uses pointer magic to avoid a second allocation for the actual data. The comment in the string header is useful.<br> </div> Tue, 05 Nov 2013 13:30:36 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572828/ https://lwn.net/Articles/572828/ ballombe <div class="FormattedComment"> valgrind is not a C++ interpretor, but a binary interpretor where there is no notion of destructor.<br> </div> Tue, 05 Nov 2013 11:08:40 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572824/ https://lwn.net/Articles/572824/ Otus <div class="FormattedComment"> <font class="QuotedText">&gt; Yes, in practice, you are probably fine with basic objects such as the one that you show here. But for more complex objects, it does make a real difference whether destructors are run before the program terminates. And it is a good thing if valgrind warns about it.</font><br> <p> Could valgrind somehow find out whether those objects have destructors that could matter? Because it would be nice to be able to simply ignore resources you don't care about at program exit but still have valgrind warn about things that are potentially dangerous (e.g. a destructor that's supposed to clean up a lock file).<br> <p> I hate programs that spend a lot of time closing down stuff after I've told them to exit, and deallocating *memory* on shut down is completely useless.<br> </div> Tue, 05 Nov 2013 10:20:22 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572823/ https://lwn.net/Articles/572823/ edeloget <div class="FormattedComment"> Well, the C++ standard explicitely says that calling exit(3) prevents the execution of automatic variables destructors - so of course you leak the memory. See section 3.6.1 [basic.start.main] ยง4.<br> <p> bugfix 280271 fixes another problem related to the fact that the glibc implementation of std::string internal pointer does not point to the beginning of the allocated block. It uses weird (*) magic to suppress some false-positive. <br> <p> (*) as in: tied to a particular implementation ; an implementation change in either the C++ standard library or in the compiler's new[] implementation will defeat this magic. <br> </div> Tue, 05 Nov 2013 09:23:36 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572813/ https://lwn.net/Articles/572813/ gutschke <div class="FormattedComment"> I would argue that valgrind's warning message is correct, if maybe annoying.<br> <p> Valgrind verifies that you have released all (memory) resources prior to terminating the program. If you fall off the end of main(), then C++ will implicitly deallocate resources when "s" goes out of scope. But your code never gives this mechanism any chance to run.<br> <p> Yes, in practice, you are probably fine with basic objects such as the one that you show here. But for more complex objects, it does make a real difference whether destructors are run before the program terminates. And it is a good thing if valgrind warns about it.<br> <p> For production code, you could argue that there are some objects that you never want to destruct for one reason or another. And that's certainly a valid argument to make. But valgrind gives you plenty of tools to suppress warnings, if you have explicitly checked that the default behavior is inappropriate for some objects.<br> <p> So, I'd claim that valgrind is working as in intended for your example.<br> </div> Tue, 05 Nov 2013 03:13:37 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572812/ https://lwn.net/Articles/572812/ samlh <div class="FormattedComment"> <font class="QuotedText">&gt; I'm wondering if bugfix 280271 fixes that?</font><br> <p> Yes.<br> <p> Use --leak-check-heuristics=stdstring (or all) to get a result like:<br> <p> still reachable: 56 bytes in 2 blocks of which reachable via heuristic:<br> stdstring : 56 bytes in 2 blocks<br> </div> Tue, 05 Nov 2013 03:06:00 +0000 Valgrind 3.9.0 https://lwn.net/Articles/572810/ https://lwn.net/Articles/572810/ pr1268 <blockquote><font class="QuotedText">280271 Valgrind reports possible memory leaks on still-reachable std::string</font></blockquote> <p>I always like to &quot;bluff&quot; valgrind with this C++ program:</p> <pre> #include &lt;cstdlib&gt; // for exit(3) #include &lt;string&gt; #include &lt;iostream&gt; void die() { exit(0); } int main() { std::string s(&quot;Hello, World!&quot;); std::cout &lt;&lt; s &lt;&lt; std::endl; die(); return 0; } </pre> <p>With the &quot;dangling&quot; <tt>std::string</tt>, valgrind seems to think 26 bytes are &quot;possibly lost&quot;. I'm wondering if bugfix 280271 fixes that? (Or if I really should care?) ;-)</p> <p>P.S. My sincere thanks to the valgrind development team for another release.</p> Tue, 05 Nov 2013 01:48:13 +0000 Valgrind @ Fosdem 2014 https://lwn.net/Articles/572797/ https://lwn.net/Articles/572797/ mjw <div class="FormattedComment"> Valgrind will also have a developer room at Fosdem. See the Call For Participation: <a href="https://lists.fosdem.org/pipermail/fosdem/2013-October/001871.html">https://lists.fosdem.org/pipermail/fosdem/2013-October/00...</a><br> </div> Mon, 04 Nov 2013 22:32:18 +0000