Skip to content

Commit 83c5349

Browse files
committed
fix:解决TimerPool::doAfter()引起的段错误问题
1 parent f07b25d commit 83c5349

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

modules/eventx/timer_pool.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ TimerPool::TimerToken TimerPool::Impl::doEvery(const Milliseconds &m_sec, Callba
5858
return TimerToken();
5959
}
6060

61-
auto this_timer = wp_loop_->newTimerEvent("TimerPool::doEvery");
62-
auto new_token = timers_.alloc(this_timer);
63-
this_timer->initialize(m_sec, event::Event::Mode::kPersist);
64-
this_timer->setCallback(std::move(cb));
65-
this_timer->enable();
66-
return new_token;
61+
auto timer = wp_loop_->newTimerEvent("TimerPool::doEvery");
62+
auto token = timers_.alloc(timer);
63+
timer->initialize(m_sec, event::Event::Mode::kPersist);
64+
timer->setCallback(std::move(cb));
65+
timer->enable();
66+
return token;
6767
}
6868

6969
TimerPool::TimerToken TimerPool::Impl::doAfter(const Milliseconds &m_sec, Callback &&cb)
@@ -73,25 +73,26 @@ TimerPool::TimerToken TimerPool::Impl::doAfter(const Milliseconds &m_sec, Callba
7373
return TimerToken();
7474
}
7575

76-
auto this_timer = wp_loop_->newTimerEvent("TimerPool::doAfter");
77-
auto new_token = timers_.alloc(this_timer);
78-
this_timer->initialize(m_sec, event::Event::Mode::kOneshot);
76+
auto timer = wp_loop_->newTimerEvent("TimerPool::doAfter");
77+
auto token = timers_.alloc(timer);
78+
timer->initialize(m_sec, event::Event::Mode::kOneshot);
7979

8080
#if __cplusplus >= 201402L
81-
this_timer->setCallback([loop = wp_loop_, moved_cb = std::move(cb), this_timer] {
82-
moved_cb();
83-
loop->runNext([this_timer] { delete this_timer; });
81+
timer->setCallback([this, cb = std::move(cb), token] {
82+
cb();
83+
auto timer = timers_.free(token);
84+
wp_loop_->runNext([timer] { delete timer; });
8485
});
8586
#elif __cplusplus >= 201103L
86-
auto loop = wp_loop_;
87-
this_timer->setCallback([loop, cb, this_timer] {
87+
timer->setCallback([this, cb, token] {
8888
cb();
89-
loop->runNext([this_timer] { delete this_timer; });
89+
auto timer = timers_.free(token);
90+
wp_loop_->runNext([timer] { delete timer; });
9091
});
9192
#endif
9293

93-
this_timer->enable();
94-
return new_token;
94+
timer->enable();
95+
return token;
9596
}
9697

9798
TimerPool::TimerToken TimerPool::Impl::doAt(const TimePoint &time_point, Callback &&cb)

0 commit comments

Comments
 (0)