From 13377a0f0fa8b7db93ba520a04a1fbf74424fb0a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 22 Mar 2020 11:33:35 +0100 Subject: [PATCH] src: cleanup DestroyParam when Environment exits Otherwise, this leaks memory if the weak callback is never called. PR-URL: https://github.com/nodejs/node/pull/32421 Reviewed-By: Colin Ihrig Reviewed-By: David Carlier Reviewed-By: James M Snell --- src/async_wrap.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 4d4cd01b5511c0..ea23896881cc3f 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -350,6 +350,10 @@ class DestroyParam { Global propBag; }; +static void DestroyParamCleanupHook(void* ptr) { + delete static_cast(ptr); +} + void AsyncWrap::WeakCallback(const WeakCallbackInfo& info) { HandleScope scope(info.GetIsolate()); @@ -358,6 +362,8 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo& info) { p->propBag); Local val; + p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get()); + if (!prop_bag->Get(p->env->context(), p->env->destroyed_string()) .ToLocal(&val)) { return; @@ -382,6 +388,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo& args) { p->target.Reset(isolate, args[0].As()); p->propBag.Reset(isolate, args[2].As()); p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter); + p->env->AddCleanupHook(DestroyParamCleanupHook, p); } void AsyncWrap::GetAsyncId(const FunctionCallbackInfo& args) {