Boost logo

Boost Users :

Subject: [Boost-users] boost::asio::deadline_timer destruction
From: Daniel (zin_corp_at_[hidden])
Date: 2013-04-19 11:14:44


 Hello,

I've noticed that WaitHandler can be called after boost::asio::deadline_timer destruction. It may cause using of uninitialized data. Are there any other solutions of graceful destruction of  boost::asio::deadline_timer to be sure that after this destruction WaitHandler will never be called?

Example pseudo code:

class UsingDeadlineTimer
{
  char some_data_accessed_from_the_wait_handler[512];
  boost::asio::deadline_timer timer;
  
  void WaitHandler(const boost::system::error_code  &ec)
  {
    if(ec == boost::asio::error::operation_aborted)
    {
      return;
    }
    memset(some_data_accessed_from_the_wait_handler, 'E', 512); // cause the problem, because UsingDeadlineTimer may be already destroyed.

  }

  UsingDeadlineTimer() : timer(io_service)
  {
     timer.expires_at(the moment of UsingDeadlineTimer destruction);
     timer.async_wait( boost::bind(&UsingDeadlineTimer::WaitHandler, this, boost::asio::placeholders::error) );
  }

  ~UsingDeadlineTimer()
  {
     timer.cancel(); // but we already can be in the WaitHandler body which will access some_data_accessed_from_the_wait_handler 
  }

};

Thank you,
Daniel 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net