Browse Source

[trac957] Add assertion check to detect use-after-free type of bugs

Yoshitaka Aharen 14 years ago
parent
commit
3e1fae0b23
1 changed files with 7 additions and 2 deletions
  1. 7 2
      src/lib/asiolink/interval_timer.cc

+ 7 - 2
src/lib/asiolink/interval_timer.cc

@@ -53,14 +53,18 @@ private:
     long interval_;
     // asio timer
     asio::deadline_timer timer_;
+    // interval_ will be set to this value in destructor in order to detect
+    // use-after-free type of bugs.
+    static const long INVALIDATED_INTERVAL = -1;
 };
 
 IntervalTimerImpl::IntervalTimerImpl(IOService& io_service) :
     interval_(0), timer_(io_service.get_io_service())
 {}
 
-IntervalTimerImpl::~IntervalTimerImpl()
-{}
+IntervalTimerImpl::~IntervalTimerImpl() {
+    interval_ = INVALIDATED_INTERVAL;
+}
 
 void
 IntervalTimerImpl::setup(const IntervalTimer::Callback& cbfunc,
@@ -102,6 +106,7 @@ IntervalTimerImpl::update() {
 
 void
 IntervalTimerImpl::callback(const asio::error_code& cancelled) {
+    assert(interval_ != INVALIDATED_INTERVAL);
     // Do not call cbfunc_ in case the timer was cancelled.
     // The timer will be canelled in the destructor of asio::deadline_timer.
     if (!cancelled) {