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 authored and targos committed Apr 22, 2020
1 parent 52a52d2 commit aaf94fd
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 @@ -348,6 +348,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 @@ -356,6 +360,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 @@ -380,6 +386,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);
}


Expand Down

0 comments on commit aaf94fd

Please sign in to comment.