title: How to delete boost io_service
body: <p><strong>My simplified question</strong><br>
  I read <a href="https://stackoverflow.com/questions/2314816/remove-all-handlers-from-a-boostasioio-service-without-calling-them">this thread</a> and I am trying to delete the io_service object. I do this</p>
  
  <pre><code>m_IO.stop();
  m_IO.~io_service();
  </code></pre>
  
  <p>m_IO is an object of <code>boost::asio::io_service</code>. I found that my thread was blocked by <code>m_IO.~io_service();</code> How can I delete <code>io_service</code>?<br><br>
  <strong>My Complete question</strong><br>
  I am making a daily timer by using boost io_service and deadline timer. The problem is when I want to delete my daily timer, my thread will disappear when it try to delete boost io_service.<br></p>
  
  <p><code>main.cpp</code></p>
  
  <pre><code>int main()
  {
      myDailyTimer* pTimer = new myDailyTimer;
      // do something
      delete pTimer;
      return 0;
  }
  </code></pre>
  
  <p>I set break points in <code>myDailyTimer.cpp::int i = 0;</code> and <code>myDailyTimer.cpp::int j = 0;</code> and <code>main::return 0;</code> My main thread can reach <code>int i = 0;</code>, My timer thread cannot reach <code>int j = 0;</code>, My main thread cannot reach <code>return 0;</code>.<br><br>
  I found the my main thread will disappear when it try to delete <code>boost::asio::io_service</code> object. How to solve this problem? Am I using <code>boost::asio::io_service</code> in a wrong way?</p>
  
  <p><code>myDailyTimer.h</code></p>
  
  <pre><code>class myDailyTimerInterface
  {
  public:
      myDailyTimerInterface(){}
      ~myDailyTimerInterface(){}
  
      virtual void TimerCallback(int nTimerID) = 0;
  };
  class myDailyTimer :
      public myThread
  {
  public:
      boost::asio::io_service m_IO;
      boost::asio::deadline_timer * m_pTimer;
      tm m_tmSpecificTime;
      std::string m_strSpecificTime;
      int m_nTimerID;
      myDailyTimerInterface* m_pParent;
  public:
      myDailyTimer();
      ~myDailyTimer();
  
      void SetTime(tm strIN, int nID); // msec
      void TimerCallback();
  
      //Override
      void ThreadMain();
  protected:
      std::string MakeStringSpecificTime();
      void AddOneDay();
  };
  </code></pre>
  
  <p><br>
  <code>myDailyTimer.cpp</code></p>
  
  <pre><code>myDailyTimer::myDailyTimer()
  {
      m_pTimer = 0;
      m_strSpecificTime = "";
  }
  
  myDailyTimer::~myDailyTimer()
  {
      EndThread();
      if (m_pTimer != 0)
      {
          m_pTimer-&gt;cancel();
          delete m_pTimer;
      }
      m_IO.stop();
      m_IO.~io_service();
      int i = 0;
      i++;
  }
  
  void myDailyTimer::SetTime(tm tmIN, int nID) // msec
  {
      if (m_pTimer != 0)
      {
          m_pTimer-&gt;cancel();
          delete m_pTimer;
      }
      m_tmSpecificTime = tmIN;
      m_strSpecificTime = MakeStringSpecificTime();
      m_nTimerID = nID;
      m_pTimer = new boost::asio::deadline_timer(m_IO, boost::posix_time::time_from_string(m_strSpecificTime));
      m_pTimer-&gt;async_wait(boost::bind(&amp;myDailyTimer::TimerCallback, this));
  
      myThread::Start();
  }
  
  std::string myDailyTimer::MakeStringSpecificTime()
  {
      time_t localTime;
      localTime = mktime(&amp;m_tmSpecificTime); // time is GMT local
      struct tm * ptm = gmtime(&amp;localTime); // convert time to GMT +0
      char veccNextTime[64];
      memset(veccNextTime, 0, sizeof(veccNextTime));
      sprintf(veccNextTime, "%d-%02d-%02d %02d:%02d:%02d.000",
          ptm-&gt;tm_year + 1900, ptm-&gt;tm_mon + 1, ptm-&gt;tm_mday,
          ptm-&gt;tm_hour, ptm-&gt;tm_min, ptm-&gt;tm_sec);
      std::string strTemp(veccNextTime);
      return strTemp;
  }
  
  void myDailyTimer::AddOneDay()
  {
      m_tmSpecificTime.tm_mday += 1;
      mktime(&amp;m_tmSpecificTime); /* normalize result */
  }
  
  void myDailyTimer::TimerCallback()
  {
      if (m_pParent != 0)
          m_pParent-&gt;TimerCallback(m_nTimerID);
      //m_timer-&gt;expires_from_now(boost::posix_time::milliseconds(m_nTimerDuration));
      AddOneDay();
      m_strSpecificTime = MakeStringSpecificTime();
      m_pTimer-&gt;expires_at(boost::posix_time::time_from_string(m_strSpecificTime));
      m_pTimer-&gt;async_wait(boost::bind(&amp;myDailyTimer::TimerCallback, this));
  }
  
  //Override
  void myDailyTimer::ThreadMain()
  {
      while (!IsEndThread())
          m_IO.run();
      int j = 0;
      j++;
  }
  </code></pre>
  
  <p><br></p>