Boost C++ Libraries: Ticket #4368: boost::recursive_mutex constructor potential resource leak https://svn.boost.org/trac10/ticket/4368 <p> When running the Klocwork static code analysis tool on some code that uses Boost.Thread, Klocwork reported the following potential issue (note that this is for boost 1_36_0, but the latest trunk code appears to have the same issue): </p> <hr /> <p> boost-1_36/boost/thread/pthread/recursive_mutex.hpp: 50<br /> Resource acquired to 'attr' at line 42 may be lost here. Also there is one similar error on line 56.<br /> recursive_mutex.hpp<a class="missing ticket">#1:42</a>: Resource is acquired: 'attr' is accessed via 1 argument of the call to function 'pthread_mutexattr_init'<br /> recursive_mutex.hpp<a class="missing ticket">#1:43</a>: init_attr_res is false<br /> recursive_mutex.hpp<a class="missing ticket">#1:47</a>: 'attr' might be changed<br /> recursive_mutex.hpp<a class="missing ticket">#1:48</a>: set_attr_res is true<br /> recursive_mutex.hpp<a class="missing ticket">#1:50</a>: Resource is lost: 'attr' </p> <hr /> <p> I'm not absolutely certain this is the issue Klocwork is attempting to report, but in the boost/thread/pthread/recursive_mutex.hpp header file the mutex attribute object is not destroyed if an exception is thrown due to pthread_mutexattr_settype() failure. I see that pthread_mutexattr_destroy() has been added since 1_36_0 for the case where pthread_mutex_init() fails. A similar call is needed if pthread_mutexattr_settype() fails. </p> <pre class="wiki"> recursive_mutex() { #ifdef BOOST_PTHREAD_HAS_MUTEXATTR_SETTYPE pthread_mutexattr_t attr; int const init_attr_res=pthread_mutexattr_init(&amp;attr); if(init_attr_res) { boost::throw_exception(thread_resource_error()); } int const set_attr_res=pthread_mutexattr_settype(&amp;attr,PTHREAD_MUTEX_RECURSIVE); if(set_attr_res) { // Shouldn't the following line be added here? // BOOST_VERIFY(!pthread_mutexattr_destroy(&amp;attr)); boost::throw_exception(thread_resource_error()); } int const res=pthread_mutex_init(&amp;m,&amp;attr); if(res) { BOOST_VERIFY(!pthread_mutexattr_destroy(&amp;attr)); boost::throw_exception(thread_resource_error()); } BOOST_VERIFY(!pthread_mutexattr_destroy(&amp;attr)); </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4368 Trac 1.4.3 Anthony Williams Fri, 09 Jul 2010 22:00:55 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4368#comment:1 https://svn.boost.org/trac10/ticket/4368#comment:1 <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">fixed</span> </li> </ul> <p> Fixed on trunk </p> Ticket