From aaf94fd6bbf20798d75a30fef5c2c1645f1d604b 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 10f78333003f76..e712235d861df9 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -348,6 +348,10 @@ class DestroyParam { Global propBag; }; +static void DestroyParamCleanupHook(void* ptr) { + delete static_cast(ptr); +} + void AsyncWrap::WeakCallback(const WeakCallbackInfo& info) { HandleScope scope(info.GetIsolate()); @@ -356,6 +360,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; @@ -380,6 +386,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); }