Boost C++ Libraries: Ticket #5754: Bug/design issue with boost::thread::id https://svn.boost.org/trac10/ticket/5754 <p> Hi, </p> <p> I have just run into a nasty memory leak which was caused by a circular reference due to boost::thread::id. </p> <p> The situation is as follows: </p> <ol><li>Main thread controls several worker thread. </li><li>Only one worker thread shall be allowed to report its current status, so we need some identifier. </li><li>The information which thread shall report, is placed as a boost::thread::id into a structure which is shared by main thread and all worker threads (via std::shared_ptr) </li></ol><ol start="4"><li>We happily and unwittingly(!) introduced a circular reference! </li></ol><p> Analysis: Main thread successfully joins all worker threads, still there is a circular reference. Simple reason: boost::thread::id has partial ownership of the corresponding thread local storage! Technically it's implemented as an intrusive_ptr on boost::detail::thread_data which owns the TLS, in my case it's the data of a full blown function object, which I had passed to boost::thread. </p> <p> In the example above, one of the worker threads shares ownership to the shared structure containing a thread::id which shares ownership of...surprise ...himself! </p> <p> This is IMHO a design bug! A thread::id should not have any ownership of its corresponding thread's data, but should be a plain vanilla value type without any side-effects. </p> <p> Fix is simple, just remove the thread::id's ownership semantics. I really hope this gets addresses (soon), since it's fundamental, and IMHO in contrast to reasonable expectations. </p> <p> Tested with boost v1.47 on MingW/GCC and VC 2010 64-bit </p> <p> Best regards, <a class="missing wiki">ZenJu</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5754 Trac 1.4.3 viboes Sun, 11 Dec 2011 09:53:49 GMT owner, status changed; keywords set https://svn.boost.org/trac10/ticket/5754#comment:1 https://svn.boost.org/trac10/ticket/5754#comment:1 <ul> <li><strong>keywords</strong> thread::id added </li> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I was wondering what prevent from making thread::id an opaque type around the thread::native_handle, Anthony? Shouldn't the native handle solve this issue? </p> Ticket viboes Sun, 11 Dec 2011 09:57:04 GMT type changed; cc set https://svn.boost.org/trac10/ticket/5754#comment:2 https://svn.boost.org/trac10/ticket/5754#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> added </li> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Support Requests</span> </li> </ul> <p> Can this be considered a duplicate of <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4345" title="#4345: Bugs: thread::id and joining problem with cascade of threads (closed: fixed)">#4345</a> thread::id and joining problem with cascade of threads? </p> <p> Moved to support request until clarified. </p> Ticket viboes Sat, 31 Dec 2011 15:09:54 GMT status, type changed; resolution set https://svn.boost.org/trac10/ticket/5754#comment:3 https://svn.boost.org/trac10/ticket/5754#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>type</strong> <span class="trac-field-old">Support Requests</span> → <span class="trac-field-new">Bugs</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">duplicate</span> </li> </ul> <p> duplicate of <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4345" title="#4345: Bugs: thread::id and joining problem with cascade of threads (closed: fixed)">#4345</a> thread::id and joining problem with cascade of threads </p> Ticket anonymous Thu, 08 Mar 2012 16:16:02 GMT <link>https://svn.boost.org/trac10/ticket/5754#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5754#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5754#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> duplicate of <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4345" title="#4345: Bugs: thread::id and joining problem with cascade of threads (closed: fixed)">#4345</a> thread::id and joining problem with cascade of threads </p> </blockquote> <p> I have encountered another manifestation of this issue. I have produced a very simple test program that will exhibit the issue: </p> <pre class="wiki">#include &lt;boost/thread.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; void GetThreadId(boost::shared_ptr&lt;boost::thread::id&gt; tidPtr) { *tidPtr = boost::this_thread::get_id(); } int main(int argc, char* argv[]) { for(;;) { boost::shared_ptr&lt;boost::thread::id&gt; tidPtr(new boost::thread::id()); boost::thread thread(GetThreadId, tidPtr); tidPtr.reset(); // force necessity to pass tidPtr by value thread.join(); } return 0; } </pre><p> This program leaks memory every time around the for loop in main. </p> </description> <category>Ticket</category> </item> </channel> </rss>