title: Boost.thread possible memory leak
body: <p>Since I've started making a little project aiming to have a crossplatform support, I chose boost 1.47 to interact with the underlying OS. My project needed some multithreading, so I made a little wrapper over boost threads to fulfill my needs.</p>
  
  <p>Little I knew, boost apparently leaves the thread on memory after destructing its object(?), or then it may have some sort of memory leak possibility.</p>
  
  <p>The implementation of my wrapper has a scoped_ptr of type thread, and the scoped ptr will get initialized when one calls the start() function in the wrapper class. The running thread will be stopped from main thread using thread->interrupt(), and the destructor will be called from the wrapper function. (Destructor of the thread's procedure structure, which has operator()() in it.</p>
  
  <p>Here's the implementation of the wrapper class:
  (note: i_exception and couple of other functions are parts of other project components)</p>
  
  <pre><code>#define TIMED_JOIN boost::posix_time::milliseconds(1)
  
  namespace utils
  {
      struct thread_threadable
      {
          template&lt;typename T&gt; friend class ut_thread;
      private:
          boost::shared_ptr&lt;thread_threadable&gt; instance;
      public:
          virtual ~thread_threadable() {}
          virtual void operator()() = 0;
      };
  
      template&lt;typename T = thread_threadable&gt;
      class ut_thread
      {
      public:
          typedef T proc_t;
      private:
          boost::scoped_ptr&lt;boost::thread&gt; thr;
          boost::shared_ptr&lt;proc_t&gt; proc;
      public:
          explicit ut_thread(const boost::shared_ptr&lt;proc_t&gt; &amp;procedure) : proc(procedure) {}
          ~ut_thread();
  
          void start();
          void stop();
  
          bool running() const {return this-&gt;thr.get() != NULL;}
          proc_t &amp;procedure() const
          {
              BOOST_ASSERT(this-&gt;proc.get() != NULL);
              return *this-&gt;proc;
          }
      };
  }
  typedef utils::thread_threadable threadable;
  
  template&lt;typename T&gt;
  utils::ut_thread&lt;T&gt;::~ut_thread()
  {
      if(this-&gt;thr.get() != NULL)
      {
          BOOST_ASSERT(this-&gt;proc.get() != NULL);
          this-&gt;stop();
      }
  }
  template&lt;typename T&gt;
  void utils::ut_thread&lt;T&gt;::start()
  {
      if(this-&gt;thr.get() != NULL)
          i_exception::throw_this("another thread of this procedure is already running");
      if(this-&gt;proc.get() == NULL)
          i_exception::throw_this("procedure object not initialized");
  
      this-&gt;proc-&gt;instance = this-&gt;proc;
  
      this-&gt;thr.reset(new boost::thread(boost::ref(*this-&gt;proc)));
      this-&gt;thr-&gt;timed_join(TIMED_JOIN);
  }
  template&lt;typename T&gt;
  void utils::ut_thread&lt;T&gt;::stop()
  {
      if(this-&gt;thr.get() == NULL)
          i_exception::throw_this("no thread was running");
  
      this-&gt;thr-&gt;interrupt();
      this-&gt;proc-&gt;~T();
      this-&gt;thr.reset(NULL);
  }
  </code></pre>
  
  <p>And then by checking the functionality of this wrapper class, I made test to main.cpp:</p>
  
  <pre><code>struct my_thr : public utils::thread_threadable
  {
      void operator()()
      {
          while(true);
      }
  };
  
  int main()
  {
      while(true)
      {
          utils::ut_thread&lt;&gt; thr(boost::shared_ptr&lt;threadable&gt;(new my_thr));
          utils::ut_thread&lt;&gt; thr1(boost::shared_ptr&lt;threadable&gt;(new my_thr));
          thr.start();
          thr1.start();
          boost::this_thread::sleep(boost::posix_time::seconds(1));
      }
      return 0;
  }
  </code></pre>
  
  <p>At which point I noticed that these threads do not destruct, they will stay in memory until program gets terminated. They also keep executing the 'while(true)' statement.</p>
  
  <p>So I'm asking, what would cause this kind of behaviour? Is it something defined, or just a bug or something else?</p>