Opened 10 years ago
Closed 5 years ago
#7379 closed Bugs (worksforme)
Boost Format strings causing memory leaks
| Reported by: | Owned by: | James E. King, III | |
|---|---|---|---|
| Milestone: | Boost 1.66.0 | Component: | format | 
| Version: | Boost 1.44.0 | Severity: | Problem | 
| Keywords: | format memory leak | Cc: | 
Description
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.
The two format strings I was using were:
L"%1$.1f"
- format with one decimal place of precision
 
L"%1$.0f %%"
- format with no decimal places but add a percent sign
 
I determined that removing the "%%" reduced some of the memory leaks, and removing the "$.1f" or "$.0f" eliminated the remaining memory leaks.
This was done using Boost v 1.44 with Visual Studio 2010 on Win 7 64-bit.
For more context here is part of the code that I can share:
note, mFormatter is a boost::wformat member object
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
}
      Change History (6)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
No, the format string is correct. The "%N$" syntax means to read the Nth argument instead of processing the arguments sequentially.
comment:3 by , 5 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
I am unable to reproduce this issue using valgrind by adding the following test to format_test_wstring.cpp:
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;
 }
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".
comment:4 by , 5 years ago
| Milestone: | To Be Determined → Boost 1.66.0 | 
|---|---|
| Resolution: | → invalid | 
| Status: | assigned → closed | 
comment:5 by , 5 years ago
| Resolution: | invalid | 
|---|---|
| Status: | closed → reopened | 
comment:6 by , 5 years ago
| Resolution: | → worksforme | 
|---|---|
| Status: | reopened → closed | 

It looks like bad format string. Try to remove leading "1" (L"%$.1f").