Skip to content

Commit da8414e

Browse files
fhinkelMylesBorins
authored andcommitted
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: #17020 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 91385be commit da8414e

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/inspector_agent.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,19 @@ class InspectorTimer {
261261
}
262262

263263
static void TimerClosedCb(uv_handle_t* uvtimer) {
264-
InspectorTimer* timer =
264+
std::unique_ptr<InspectorTimer> timer(
265265
node::ContainerOf(&InspectorTimer::timer_,
266-
reinterpret_cast<uv_timer_t*>(uvtimer));
267-
delete timer;
266+
reinterpret_cast<uv_timer_t*>(uvtimer)));
267+
// Unique_ptr goes out of scope here and pointer is deleted.
268268
}
269269

270270
~InspectorTimer() {}
271271

272272
uv_timer_t timer_;
273273
V8InspectorClient::TimerCallback callback_;
274274
void* data_;
275+
276+
friend std::unique_ptr<InspectorTimer>::deleter_type;
275277
};
276278

277279
class InspectorTimerHandle {

src/inspector_io.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ int CloseAsyncAndLoop(uv_async_t* async) {
114114

115115
// Delete main_thread_req_ on async handle close
116116
void ReleasePairOnAsyncClose(uv_handle_t* async) {
117-
AsyncAndAgent* pair = node::ContainerOf(&AsyncAndAgent::first,
118-
reinterpret_cast<uv_async_t*>(async));
119-
delete pair;
117+
std::unique_ptr<AsyncAndAgent> pair(node::ContainerOf(&AsyncAndAgent::first,
118+
reinterpret_cast<uv_async_t*>(async)));
119+
// Unique_ptr goes out of scope here and pointer is deleted.
120120
}
121121

122122
} // namespace

0 commit comments

Comments
 (0)