Boost C++ Libraries: Ticket #7379: Boost Format strings causing memory leaks https://svn.boost.org/trac10/ticket/7379 <p> I determined that certain format string used in a boost::wformat object will leak memory. I was using a formatter to convert a value to a floating point value with and without a percent symbol at the end. </p> <p> The two format strings I was using were: </p> <pre class="wiki">L"%1$.1f" </pre><ul><li>format with one decimal place of precision<br /> </li></ul><pre class="wiki">L"%1$.0f %%" </pre><ul><li>format with no decimal places but add a percent sign </li></ul><p> I determined that removing the "%%" reduced some of the memory leaks, and removing the "$.1f" or "$.0f" eliminated the remaining memory leaks. </p> <p> This was done using Boost v 1.44 with Visual Studio 2010 on Win 7 64-bit. </p> <p> For more context here is part of the code that I can share: </p> <p> note, mFormatter is a boost::wformat member object </p> <pre class="wiki">MyObject::MyObject() : mNumDecimalPlaces (1), mFormatter (L"%1$.1f") //--- THIS LEAKED { } std::wstring MyObject::PercentToString(const FLOAT iMin, const FLOAT iMax, const FLOAT iPercent) { FLOAT value = PercentToValue(iMin, iMax, iPercent); return boost::str(mFormatter % value); //--- THIS LEAKED } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7379 Trac 1.4.3 Jan Vonasek <jan.vonasek@…> Fri, 19 Oct 2012 10:37:28 GMT <link>https://svn.boost.org/trac10/ticket/7379#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7379#comment:1</guid> <description> <p> It looks like bad format string. Try to remove leading "1" (L"%$.1f"). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Fri, 19 Oct 2012 14:47:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7379#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7379#comment:2</guid> <description> <p> No, the format string is correct. The "%N$" syntax means to read the Nth argument instead of processing the arguments sequentially. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 12 Oct 2017 15:17:42 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/7379#comment:3 https://svn.boost.org/trac10/ticket/7379#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Samuel Krempp</span> to <span class="trac-author">James E. King, III</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I am unable to reproduce this issue using valgrind by adding the following test to format_test_wstring.cpp: </p> <pre class="wiki">diff --git a/test/format_test_wstring.cpp b/test/format_test_wstring.cpp index c30f740..4f9a871 100644 --- a/test/format_test_wstring.cpp +++ b/test/format_test_wstring.cpp @@ -29,8 +29,16 @@ int test_main(int, char* []) BOOST_ERROR("Basic w-parsing Failed"); if(str( wformat(L"%%##%#x ##%%1 %s00") % 20 % L"Escaped OK" ) != L"%##0x14 ##%1 Escaped OK00") BOOST_ERROR("Basic wp-parsing Failed") ; -#endif // wformat tests + // testcase for https://svn.boost.org/trac10/ticket/7379 (for valgrind) + wformat wfmt(L"%1$.1f"); + std::wstring ws = str(wfmt % 123.45f); + BOOST_CHECK_EQUAL(ws.compare(L"123.4"), 0); + wformat wfmt2(L"%1$.0f %%"); + std::wstring ws2 = (wfmt2 % 123.45f).str(); + BOOST_CHECK_EQUAL(ws2.compare(L"123 %"), 0); + +#endif // wformat tests return 0; } </pre><p> The method in which memory leak detection was determined is missing from the original bug report.After building debug and then running valgrind on it, there were no leaks. As such I am resolving this as "worksforme". </p> Ticket James E. King, III Thu, 12 Oct 2017 15:18:16 GMT status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/7379#comment:4 https://svn.boost.org/trac10/ticket/7379#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.66.0</span> </li> </ul> Ticket James E. King, III Thu, 12 Oct 2017 15:18:36 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/7379#comment:5 https://svn.boost.org/trac10/ticket/7379#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> Ticket James E. King, III Thu, 12 Oct 2017 15:18:46 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7379#comment:6 https://svn.boost.org/trac10/ticket/7379#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> Ticket