@@ -54,6 +54,7 @@ bool AsioEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
54
54
55
55
QCoreApplication::sendPostedEvents ();
56
56
57
+ // When calling method sendPostedEvents, the state of variable may change, so we check it.
57
58
if (interrupted_.load (std::memory_order_relaxed))
58
59
return false ;
59
60
@@ -106,8 +107,6 @@ void AsioEventDispatcher::unregisterSocketNotifier(QSocketNotifier* /* notifier
106
107
void AsioEventDispatcher::registerTimer (
107
108
int id, int interval, Qt::TimerType type, QObject* object)
108
109
{
109
- DCHECK (object);
110
-
111
110
TimePoint start_time = Clock::now ();
112
111
TimePoint expire_time = start_time + Milliseconds (interval);
113
112
@@ -132,8 +131,6 @@ bool AsioEventDispatcher::unregisterTimer(int id)
132
131
// --------------------------------------------------------------------------------------------------
133
132
bool AsioEventDispatcher::unregisterTimers (QObject* object)
134
133
{
135
- DCHECK (object);
136
-
137
134
auto it = timers_.begin ();
138
135
bool removed = false ;
139
136
@@ -157,14 +154,11 @@ bool AsioEventDispatcher::unregisterTimers(QObject* object)
157
154
}
158
155
159
156
// --------------------------------------------------------------------------------------------------
160
- QList<QAbstractEventDispatcher::TimerInfo> AsioEventDispatcher::registeredTimers (
161
- QObject* object) const
157
+ QList<QAbstractEventDispatcher::TimerInfo> AsioEventDispatcher::registeredTimers (QObject* object) const
162
158
{
163
- DCHECK (object);
164
-
165
159
QList<TimerInfo> list;
166
160
167
- for (auto it = timers_.cbegin (); it ! = timers_.cend (); ++it)
161
+ for (auto it = timers_.cbegin (), it_end = timers_.cend (); it != it_end ; ++it)
168
162
{
169
163
if (it->second .object == object)
170
164
list.append ({ it->second .id , it->second .interval , it->second .type });
@@ -239,6 +233,7 @@ void AsioEventDispatcher::unregisterEventNotifier(QWinEventNotifier* notifier)
239
233
// --------------------------------------------------------------------------------------------------
240
234
void AsioEventDispatcher::wakeUp ()
241
235
{
236
+ // Send an empty lambda so that call run_one inside method processEvents completes.
242
237
asio::post (io_context_, []{});
243
238
}
244
239
@@ -294,12 +289,14 @@ void AsioEventDispatcher::asyncWaitForNextTimer()
294
289
if (timers_.empty ())
295
290
return ;
296
291
292
+ // Find the timer that should be completed before all others.
297
293
auto next_expire_timer = std::min_element (timers_.begin (), timers_.end (),
298
294
[](const auto & lhs, const auto & rhs)
299
295
{
300
296
return lhs.second .expire_time < rhs.second .expire_time ;
301
297
});
302
298
299
+ // Start waiting for the timer.
303
300
timer_.expires_at (next_expire_timer->second .expire_time );
304
301
timer_.async_wait (std::bind (
305
302
&AsioEventDispatcher::onTimerEvent, this , std::placeholders::_1, next_expire_timer->second .id ));
@@ -317,6 +314,7 @@ void AsioEventDispatcher::onTimerEvent(const std::error_code& error_code, int id
317
314
318
315
QCoreApplication::sendEvent (it->second .object , new QTimerEvent (id));
319
316
317
+ // When calling method sendEvent the timer may have been deleted, so we look for it again.
320
318
it = timers_.find (id);
321
319
if (it == timers_.end ())
322
320
return ;
0 commit comments