Skip to content

Commit

Permalink
src: cleanup DestroyParam when Environment exits
Browse files Browse the repository at this point in the history
Otherwise, this leaks memory if the weak callback is never called.

PR-URL: #32421
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Mar 30, 2020
1 parent eda165f commit 13377a0
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ class DestroyParam {
Global<Object> propBag;
};

static void DestroyParamCleanupHook(void* ptr) {
delete static_cast<DestroyParam*>(ptr);
}

void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
HandleScope scope(info.GetIsolate());

Expand All @@ -358,6 +362,8 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
p->propBag);
Local<Value> val;

p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get());

if (!prop_bag->Get(p->env->context(), p->env->destroyed_string())
.ToLocal(&val)) {
return;
Expand All @@ -382,6 +388,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
p->target.Reset(isolate, args[0].As<Object>());
p->propBag.Reset(isolate, args[2].As<Object>());
p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter);
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
}

void AsyncWrap::GetAsyncId(const FunctionCallbackInfo<Value>& args) {
Expand Down

0 comments on commit 13377a0

Please sign in to comment.