Skip to content

Commit 13377a0

Browse files
committed
src: cleanup DestroyParam when Environment exits
Otherwise, this leaks memory if the weak callback is never called. PR-URL: #32421 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent eda165f commit 13377a0

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
@@ -350,6 +350,10 @@ class DestroyParam {
350350
Global<Object> propBag;
351351
};
352352

353+
static void DestroyParamCleanupHook(void* ptr) {
354+
delete static_cast<DestroyParam*>(ptr);
355+
}
356+
353357
void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
354358
HandleScope scope(info.GetIsolate());
355359

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

365+
p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get());
366+
361367
if (!prop_bag->Get(p->env->context(), p->env->destroyed_string())
362368
.ToLocal(&val)) {
363369
return;
@@ -382,6 +388,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
382388
p->target.Reset(isolate, args[0].As<Object>());
383389
p->propBag.Reset(isolate, args[2].As<Object>());
384390
p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter);
391+
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
385392
}
386393

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

0 commit comments

Comments
 (0)