Boost C++ Libraries: Ticket #8079: Chrono memory leak https://svn.boost.org/trac10/ticket/8079 <p> The constructor for duration_units_default_initializer_t (in chrono/io/duration_units.hpp) allocates two string_type arrays from the heap but never deletes them. The Visual C++ MFC debugger reports this as a memory leak (and I expect most other leak detectors will as well). </p> <p> These variables are class static and only one instance of this class (per CharT) is allocated anyway, so this is not a "serious" memory leak, but any memory leak reported by a leak detector is worth fixing to avoid masking other more serious memory leaks. </p> <p> I suggest adding a destructor similar to this: </p> <pre class="wiki"> ~duration_units_default_initializer_t() { if (duration_units_default_holder&lt;CharT&gt;::initialized_) { delete[] duration_units_default_holder&lt;CharT&gt;::n_d_valid_units_; duration_units_default_holder&lt;CharT&gt;::n_d_valid_units_ = 0; delete[] duration_units_default_holder&lt;CharT&gt;::valid_units_; duration_units_default_holder&lt;CharT&gt;::valid_units_ = 0; duration_units_default_holder&lt;CharT&gt;::initialized_ = false; } } </pre><p> Further suggestions for this code: </p> <ol><li>Make the class static member variables n_d_valid_units_ and valid_units_ into non-static member variables. Only one object of this type is created, so making them them regular member variables makes things clearer and simplifies the code. </li></ol><ol start="2"><li>Do away with the initialized_ member variable entirely. If you make the other member variables non-static, there is no need for this variable. </li></ol><ol start="3"><li>If I am wrong and it is possible to create more than one instance of duration_units_default_initializer_t&lt;CharT&gt;, then you will need to make the destructor more intelligent and use smart pointers or a reference count to know when to destroy the static member variables. </li></ol> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8079 Trac 1.4.3 viboes Sun, 17 Mar 2013 22:42:04 GMT <link>https://svn.boost.org/trac10/ticket/8079#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8079#comment:1</guid> <description> <p> You are not wrong, there is only one instance. </p> <p> Changing all the static members to non-static implies to store a pointer to such a single object. Maybe this could be more elegant, but this adds an indirection. I would not be against a patch, but currently I have no time for such a refactoring. </p> <p> Have you checked your suggestion of deleting the allocated memory on the destructor in a multi DLL environment? </p> </description> <category>Ticket</category> </item> <item> <author>dm413-boost@…</author> <pubDate>Mon, 18 Mar 2013 19:54:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8079#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8079#comment:2</guid> <description> <p> I have not tested in a multi-DLL environment, I have been working with statically linked boost. Off-hand I don't see a problem -- wouldn't each DLL have it's own copy of the static variables? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 20 Mar 2013 22:11:55 GMT</pubDate> <title>status, milestone changed https://svn.boost.org/trac10/ticket/8079#comment:3 https://svn.boost.org/trac10/ticket/8079#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> <p> Your are right. Committed in trunk <a class="changeset" href="https://svn.boost.org/trac10/changeset/83510" title="Chrono: avoid memory leak.">[83510]</a>. </p> Ticket viboes Thu, 28 Mar 2013 23:50:23 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8079#comment:4 https://svn.boost.org/trac10/ticket/8079#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">fixed</span> </li> </ul> <p> Merged revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/83624" title="Chrono: merge from trunk 1.54.">[83624]</a>. </p> Ticket