Boost C++ Libraries: Ticket #9181: memory leak in Date_Time https://svn.boost.org/trac10/ticket/9181 <p> It looks like there is a memory leak when streaming Date_Time objects to standard output. Consider the following simple code: </p> <p> #include &lt;boost/date_time/posix_time/posix_time.hpp&gt; #include &lt;iostream&gt; </p> <p> using namespace boost::posix_time; </p> <p> int main() { </p> <blockquote> <p> ptime time(boost::gregorian::date(2013, 9, 30), hours(14) + minutes(42)); std::cout &lt;&lt; time &lt;&lt; '\n'; std::cout &lt;&lt; time.date() &lt;&lt; '\n'; std::cout &lt;&lt; time.date().month() &lt;&lt; '\n'; std::cout &lt;&lt; time.time_of_day() &lt;&lt; '\n'; </p> </blockquote> <p> } </p> <p> Compiling: g++ -lboost_date_time bug.cpp Testing: valgrind ./a.out </p> <p> ==8994== Memcheck, a memory error detector ==8994== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==8994== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==8994== Command: ./a.out ==8994== 2013-Sep-30 14:42:00 2013-Sep-30 Sep 14:42:00 ==8994== ==8994== HEAP SUMMARY: ==8994== in use at exit: 2,491 bytes in 49 blocks ==8994== total heap usage: 82 allocs, 33 frees, 8,448 bytes allocated ==8994== ==8994== LEAK SUMMARY: ==8994== definitely lost: 0 bytes in 0 blocks ==8994== indirectly lost: 0 bytes in 0 blocks ==8994== possibly lost: 1,187 bytes in 39 blocks ==8994== still reachable: 1,304 bytes in 10 blocks ==8994== suppressed: 0 bytes in 0 blocks ==8994== Rerun with --leak-check=full to see details of leaked memory ==8994== ==8994== For counts of detected and suppressed errors, rerun with: -v ==8994== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) </p> <p> (please find the --leak-check=full output attached). I'm using Ubuntu 12.04 LTS, Boost 1.48.0-3, and gcc 4.6.3. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9181 Trac 1.4.3 gregory@… Mon, 30 Sep 2013 12:43:46 GMT attachment set https://svn.boost.org/trac10/ticket/9181 https://svn.boost.org/trac10/ticket/9181 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">bug.out</span> </li> </ul> <p> valgrind output </p> Ticket Georg Sauthoff <mail@…> Sun, 19 Jan 2014 22:41:43 GMT cc set https://svn.boost.org/trac10/ticket/9181#comment:1 https://svn.boost.org/trac10/ticket/9181#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">mail@…</span> added </li> </ul> <p> I've reproduced your valgrind output on Fedora 19 (boost 1.53, gcc 4.8.2). </p> <p> The source seems to be </p> <p> /usr/include/boost/date_time/gregorian/gregorian_io.hpp:62 </p> <pre class="wiki">custom_date_facet* f = new custom_date_facet(); </pre><p> and </p> <p> /usr/include/boost/date_time/posix_time/posix_time_io.hpp:59 </p> <pre class="wiki">custom_ptime_facet* f = new custom_ptime_facet(); </pre><p> inside a <code>operator&lt;&lt;()</code> definition. </p> <p> But this memory leak is constant, i.e. it does not grow with the number of output operator calls - because it is guarded by: </p> <pre class="wiki">if (std::has_facet&lt;custom_ptime_facet&gt;(os.getloc())) { // use allocated facet } else { // allocate facet } </pre><p> The else branch has a comment that reads: </p> <pre class="wiki">//instantiate a custom facet for dealing with times since the user //has not put one in the stream so far. This is for efficiency //since we would always need to reconstruct for every time period //if the locale did not already exist. Of course this will be overridden //if the user imbues as some later point. </pre> Ticket Andrey Semashev Wed, 12 Mar 2014 16:33:09 GMT <link>https://svn.boost.org/trac10/ticket/9181#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9181#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9181#comment:1" title="Comment 1">Georg Sauthoff &lt;mail@…&gt;</a>: </p> <blockquote class="citation"> <p> I've reproduced your valgrind output on Fedora 19 (boost 1.53, gcc 4.8.2). </p> <p> The source seems to be </p> <p> /usr/include/boost/date_time/gregorian/gregorian_io.hpp:62 </p> <pre class="wiki">custom_date_facet* f = new custom_date_facet(); </pre><p> and </p> <p> /usr/include/boost/date_time/posix_time/posix_time_io.hpp:59 </p> <pre class="wiki">custom_ptime_facet* f = new custom_ptime_facet(); </pre><p> inside a <code>operator&lt;&lt;()</code> definition. </p> </blockquote> <p> This is not a leak. The ownership of the facet is transferred to the locale object. The facet will be deleted when the locale is. </p> <p> Regarding the valgrind report, I think this is due to the fact that the locale does not get deleted when valgrind makes its report. This is because standard streams are not destroyed at this point. </p> <p> You can try adding "std::cout.imbue(std::locale())" at the end of your main() and see if the facet is deleted then. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 12 Mar 2014 19:42:23 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9181#comment:3 https://svn.boost.org/trac10/ticket/9181#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket