title: c++ bad_weak_ptr error
body: <p>I want to create some <code>Timer</code> class, which prints "text" every N seconds, where N will be initialized in constructor.</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  
  #include &lt;iostream&gt;
  
  class Timer : public boost::enable_shared_from_this&lt;Timer&gt;
  {
  public:
      Timer ( const double interval ) : interval_sec( interval )
      {
          io_service_ = new boost::asio::io_service;
          timer_      = new boost::asio::deadline_timer ( * io_service_ );
          start( );
          io_service_ -&gt; run( );
      }
  
      void start ( )
      {
          timer_ -&gt; expires_from_now( boost::posix_time::seconds( 0 ) );
          timer_ -&gt; async_wait(boost::bind( &amp;Timer::handler
                                          , shared_from_this( )
                                          , boost::asio::placeholders::error
                                          )
                              );
      }
  
  private:
      void handler( const boost::system::error_code&amp; error )
      {
          if ( error )
          {
              std::cerr &lt;&lt; error.message( ) &lt;&lt; std::endl;
              return;
          }
  
          printf( "text\n" );
          timer_ -&gt; expires_from_now( boost::posix_time::seconds( interval_sec ) );
          timer_ -&gt; async_wait( boost::bind( &amp;Timer::handler
                                           , shared_from_this( )
                                           , boost::asio::placeholders::error
                                           )
                              );
      }
  
  private:
      boost::asio::io_service * io_service_;
      boost::asio::deadline_timer * timer_;
      double interval_sec;
  };
  
  int main()
  {
      boost::shared_ptr&lt;Timer&gt; timer( new Timer ( 10 ) );
      return 0;
  }
  </code></pre>
  
  <p>But I have <code>bad_weak_ptr</code> error.</p>
  
  <pre><code>terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::bad_weak_ptr&gt; &gt;'
    what():  tr1::bad_weak_ptr
  Aborted
  </code></pre>
  
  <p>What am I doing wrong and how can I fix it?</p>