From da8414e09a4538eeb8301f99bb67dfaf10ec8dc9 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Sun, 12 Nov 2017 21:01:01 +0100 Subject: [PATCH] src: use smart pointer instead of new and delete Use an std::unique_ptr for variables that are deleted right after creation. Since the destructor of InspectorTimer is private but needed by the unique_ptr, define deleter_type as friend. PR-URL: https://github.com/nodejs/node/pull/17020 Reviewed-By: James M Snell Reviewed-By: Timothy Gu --- src/inspector_agent.cc | 8 +++++--- src/inspector_io.cc | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index d5b3b43ec22979..215e91873a3a70 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -261,10 +261,10 @@ class InspectorTimer { } static void TimerClosedCb(uv_handle_t* uvtimer) { - InspectorTimer* timer = + std::unique_ptr timer( node::ContainerOf(&InspectorTimer::timer_, - reinterpret_cast(uvtimer)); - delete timer; + reinterpret_cast(uvtimer))); + // Unique_ptr goes out of scope here and pointer is deleted. } ~InspectorTimer() {} @@ -272,6 +272,8 @@ class InspectorTimer { uv_timer_t timer_; V8InspectorClient::TimerCallback callback_; void* data_; + + friend std::unique_ptr::deleter_type; }; class InspectorTimerHandle { diff --git a/src/inspector_io.cc b/src/inspector_io.cc index 73f0419ead6545..a870c0a2634452 100644 --- a/src/inspector_io.cc +++ b/src/inspector_io.cc @@ -114,9 +114,9 @@ int CloseAsyncAndLoop(uv_async_t* async) { // Delete main_thread_req_ on async handle close void ReleasePairOnAsyncClose(uv_handle_t* async) { - AsyncAndAgent* pair = node::ContainerOf(&AsyncAndAgent::first, - reinterpret_cast(async)); - delete pair; + std::unique_ptr pair(node::ContainerOf(&AsyncAndAgent::first, + reinterpret_cast(async))); + // Unique_ptr goes out of scope here and pointer is deleted. } } // namespace