Skip to content

Commit aaf94fd

Browse files
addaleaxtargos
authored andcommittedApr 22, 2020
src: cleanup DestroyParam when Environment exits
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>
1 parent 52a52d2 commit aaf94fd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed
 

‎src/async_wrap.cc

+7
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ class DestroyParam {
348348
Global<Object> propBag;
349349
};
350350

351+
static void DestroyParamCleanupHook(void* ptr) {
352+
delete static_cast<DestroyParam*>(ptr);
353+
}
354+
351355
void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
352356
HandleScope scope(info.GetIsolate());
353357

@@ -356,6 +360,8 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
356360
p->propBag);
357361
Local<Value> val;
358362

363+
p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get());
364+
359365
if (!prop_bag->Get(p->env->context(), p->env->destroyed_string())
360366
.ToLocal(&val)) {
361367
return;
@@ -380,6 +386,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
380386
p->target.Reset(isolate, args[0].As<Object>());
381387
p->propBag.Reset(isolate, args[2].As<Object>());
382388
p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter);
389+
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
383390
}
384391

385392

0 commit comments

Comments
 (0)
Please sign in to comment.