diff --git a/src/node.cc b/src/node.cc index ee824c5eb10c..1db39825e9c0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -62,6 +62,7 @@ #include #include +#include #if defined(NODE_HAVE_I18N_SUPPORT) #include @@ -4429,34 +4430,24 @@ void Init(int* argc, struct AtExitCallback { - AtExitCallback* next_; void (*cb_)(void* arg); void* arg_; }; -static AtExitCallback* at_exit_functions_; +static std::list at_exit_functions; // TODO(bnoordhuis) Turn into per-context event. void RunAtExit(Environment* env) { - AtExitCallback* p = at_exit_functions_; - at_exit_functions_ = nullptr; - - while (p) { - AtExitCallback* q = p->next_; - p->cb_(p->arg_); - delete p; - p = q; + for (AtExitCallback at_exit : at_exit_functions) { + at_exit.cb_(at_exit.arg_); } + at_exit_functions.clear(); } void AtExit(void (*cb)(void* arg), void* arg) { - AtExitCallback* p = new AtExitCallback; - p->cb_ = cb; - p->arg_ = arg; - p->next_ = at_exit_functions_; - at_exit_functions_ = p; + at_exit_functions.push_back(AtExitCallback{cb, arg}); }