Commit d374b0ef authored by Juho Snellman's avatar Juho Snellman

Clean up some memory management issues

- TimerEvent destructor should cancel timer.
- Fix memory leak.
parent 4eb95a09
......@@ -94,6 +94,13 @@ bool test_single_timer_no_hierarchy() {
timers.advance(10);
EXPECT_INTEQ(count, 4);
{
TimerEvent<Callback> timer2([&count] () { ++count; });
timers.schedule(&timer2, 5);
}
timers.advance(10);
EXPECT_INTEQ(count, 4);
return true;
}
......
......@@ -11,6 +11,7 @@
#include <cstdint>
#include <cstdio>
#include <limits>
#include <memory>
typedef uint64_t Tick;
......@@ -22,6 +23,10 @@ public:
TimerEventInterface() {
}
virtual ~TimerEventInterface() {
cancel();
}
void cancel();
virtual void execute() = 0;
......@@ -212,7 +217,7 @@ private:
: now_(0),
down_(down) {
if (offset + WIDTH_BITS < 64) {
up_ = new TimerWheel(offset + WIDTH_BITS, down);
up_.reset(new TimerWheel(offset + WIDTH_BITS, down));
}
}
......@@ -222,7 +227,7 @@ private:
static const int NUM_SLOTS = 1 << WIDTH_BITS;
static const int MASK = (NUM_SLOTS - 1);
TimerWheelSlot slots_[NUM_SLOTS];
TimerWheel* up_;
std::unique_ptr<TimerWheel> up_;
TimerWheel* down_;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment