@@ -357,32 +357,26 @@ class InspectorTimer {
357
357
int64_t interval_ms = 1000 * interval_s;
358
358
uv_timer_start (&timer_, OnTimer, interval_ms, interval_ms);
359
359
timer_.data = this ;
360
-
361
- env->AddCleanupHook (CleanupHook, this );
362
360
}
363
361
364
362
InspectorTimer (const InspectorTimer&) = delete;
365
363
366
364
void Stop () {
367
- env_-> RemoveCleanupHook (CleanupHook, this ) ;
365
+ if (timer_. data == nullptr ) return ;
368
366
369
- if (timer_.data == this ) {
370
- timer_.data = nullptr ;
371
- uv_timer_stop (&timer_);
372
- env_->CloseHandle (reinterpret_cast <uv_handle_t *>(&timer_), TimerClosedCb);
373
- }
367
+ timer_.data = nullptr ;
368
+ uv_timer_stop (&timer_);
369
+ env_->CloseHandle (reinterpret_cast <uv_handle_t *>(&timer_), TimerClosedCb);
374
370
}
375
371
372
+ inline Environment* env () const { return env_; }
373
+
376
374
private:
377
375
static void OnTimer (uv_timer_t * uvtimer) {
378
376
InspectorTimer* timer = node::ContainerOf (&InspectorTimer::timer_, uvtimer);
379
377
timer->callback_ (timer->data_ );
380
378
}
381
379
382
- static void CleanupHook (void * data) {
383
- static_cast <InspectorTimer*>(data)->Stop ();
384
- }
385
-
386
380
static void TimerClosedCb (uv_handle_t * uvtimer) {
387
381
std::unique_ptr<InspectorTimer> timer (
388
382
node::ContainerOf (&InspectorTimer::timer_,
@@ -405,16 +399,29 @@ class InspectorTimerHandle {
405
399
InspectorTimerHandle (Environment* env, double interval_s,
406
400
V8InspectorClient::TimerCallback callback, void * data) {
407
401
timer_ = new InspectorTimer (env, interval_s, callback, data);
402
+
403
+ env->AddCleanupHook (CleanupHook, this );
408
404
}
409
405
410
406
InspectorTimerHandle (const InspectorTimerHandle&) = delete ;
411
407
412
408
~InspectorTimerHandle () {
413
- CHECK_NOT_NULL (timer_);
414
- timer_->Stop ();
415
- timer_ = nullptr ;
409
+ Stop ();
416
410
}
411
+
417
412
private:
413
+ void Stop () {
414
+ if (timer_ != nullptr ) {
415
+ timer_->env ()->RemoveCleanupHook (CleanupHook, this );
416
+ timer_->Stop ();
417
+ }
418
+ timer_ = nullptr ;
419
+ }
420
+
421
+ static void CleanupHook (void * data) {
422
+ static_cast <InspectorTimerHandle*>(data)->Stop ();
423
+ }
424
+
418
425
InspectorTimer* timer_;
419
426
};
420
427
@@ -737,8 +744,9 @@ class NodeInspectorClient : public V8InspectorClient {
737
744
bool is_main_;
738
745
bool running_nested_loop_ = false ;
739
746
std::unique_ptr<V8Inspector> client_;
740
- std::unordered_map< int , std::unique_ptr< ChannelImpl>> channels_;
747
+ // Note: ~ ChannelImpl may access timers_ so timers_ has to come first.
741
748
std::unordered_map<void *, InspectorTimerHandle> timers_;
749
+ std::unordered_map<int , std::unique_ptr<ChannelImpl>> channels_;
742
750
int next_session_id_ = 1 ;
743
751
bool waiting_for_resume_ = false ;
744
752
bool waiting_for_frontend_ = false ;
0 commit comments