Skip to content

Commit

Permalink
src: use smart pointer instead of new and delete
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
fhinkel authored and MylesBorins committed Dec 11, 2017
1 parent 91385be commit da8414e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,19 @@ class InspectorTimer {
}

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

~InspectorTimer() {}

uv_timer_t timer_;
V8InspectorClient::TimerCallback callback_;
void* data_;

friend std::unique_ptr<InspectorTimer>::deleter_type;
};

class InspectorTimerHandle {
Expand Down
6 changes: 3 additions & 3 deletions src/inspector_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uv_async_t*>(async));
delete pair;
std::unique_ptr<AsyncAndAgent> pair(node::ContainerOf(&AsyncAndAgent::first,
reinterpret_cast<uv_async_t*>(async)));
// Unique_ptr goes out of scope here and pointer is deleted.
}

} // namespace
Expand Down

0 comments on commit da8414e

Please sign in to comment.