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