Boost C++ Libraries: Ticket #610: object destructor called four times when using thread https://svn.boost.org/trac10/ticket/610 <pre class="wiki">franzronan@yahoo.com I created this test application, I don't know if I implemented it right but I got four calls on the class test destructor... current lib version 1.32 #include &lt;boost/thread/thread.hpp&gt; #include &lt;boost/thread/xtime.hpp&gt; #include &lt;iostream&gt; using namespace std; class test { public: test() { cout &lt;&lt; "constructor" &lt;&lt; endl; }; ~test() { cout &lt;&lt; "destructor" &lt;&lt; endl; }; void operator()() { boost::xtime xt; boost::xtime_get(&amp;xt, boost::TIME_UTC); xt.sec += 5; boost::thread::sleep(xt); cout &lt;&lt; "sleep" &lt;&lt; endl; }; }; int main(int argc, char *argv[]) { //test scope { test* a = new test(); boost::thread* thrd=new boost::thread(*a); //got for desctructor calls... thrd-&gt;join(); } } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/610 Trac 1.4.3 zigmar Thu, 27 Apr 2006 15:04:51 GMT <link>https://svn.boost.org/trac10/ticket/610#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/610#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=571891 You pass the test object (a) by value, so the behaviour is probably correct. When you see, is the destruction of temporary objects. You will able to see their construction if you add custom copy constuctor to test class. Now, you print the message only when original object is created, but not when it copy-constructed. Try this: class test { public: test() { cout &lt;&lt; "constructor" &lt;&lt; endl; }; test(const test&amp;) { cout &lt;&lt; "copy constructor" &lt;&lt; endl; }; ~test() { cout &lt;&lt; "destructor" &lt;&lt; endl; }; }; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Roland Schwarz</dc:creator> <pubDate>Sun, 01 Oct 2006 13:57:47 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/610#comment:2 https://svn.boost.org/trac10/ticket/610#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> <pre class="wiki">Logged In: YES user_id=541730 This is a consequence of your code tracing only default constructor calls, and all destructor calls. If you override the default compiler generated copy constructor like: test(const test&amp; t) { *this = t; cout &lt;&lt; "copyconstructor" &lt;&lt; endl; } you will see that each constructor call indeed is matched by a destructor call. (with exception of course that you code never destroys a, which leaves a leak) </pre> Ticket