From 64dfe81b9a152c7a7f9be7454ba24744233ce61e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 22 Dec 2018 03:01:28 +0100 Subject: [PATCH] src: do not leak NodeTraceStateObserver This would otherwise be reported as a memory leak by automated tools. PR-URL: https://github.com/nodejs/node/pull/25180 Reviewed-By: Richard Lau Reviewed-By: Joyee Cheung --- src/node.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/node.cc b/src/node.cc index e9e745d9de1d90..91190788b2a35a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -198,7 +198,6 @@ class NodeTraceStateObserver : // This only runs the first time tracing is enabled controller_->RemoveTraceStateObserver(this); - delete this; } void OnTraceDisabled() override { @@ -220,8 +219,10 @@ static struct { void Initialize(int thread_pool_size) { tracing_agent_.reset(new tracing::Agent()); node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get()); - auto controller = tracing_agent_->GetTracingController(); - controller->AddTraceStateObserver(new NodeTraceStateObserver(controller)); + node::tracing::TracingController* controller = + tracing_agent_->GetTracingController(); + trace_state_observer_.reset(new NodeTraceStateObserver(controller)); + controller->AddTraceStateObserver(trace_state_observer_.get()); StartTracingAgent(); // Tracing must be initialized before platform threads are created. platform_ = new NodePlatform(thread_pool_size, controller); @@ -235,6 +236,7 @@ static struct { // Destroy tracing after the platform (and platform threads) have been // stopped. tracing_agent_.reset(nullptr); + trace_state_observer_.reset(nullptr); } void DrainVMTasks(Isolate* isolate) { @@ -287,6 +289,7 @@ static struct { return platform_; } + std::unique_ptr trace_state_observer_; std::unique_ptr tracing_agent_; tracing::AgentWriterHandle tracing_file_writer_; NodePlatform* platform_;