From 5c1d5958e03941b37662bbd4704ff82af565eb46 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 11 Jul 2019 00:12:02 +0200 Subject: [PATCH] src: add cleanup hook for ContextifyContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise there’s a memory leak left by the context when the Isolate tears down without having run the weak callback. PR-URL: https://github.com/nodejs/node/pull/28631 Reviewed-By: Colin Ihrig Reviewed-By: Jeremiah Senkpiel --- src/node_contextify.cc | 13 +++++++++++++ src/node_contextify.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index c6b9dc18de9bd0..5be774fb91af0c 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -114,6 +114,19 @@ ContextifyContext::ContextifyContext( context_.Reset(env->isolate(), v8_context.ToLocalChecked()); context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter); + env->AddCleanupHook(CleanupHook, this); +} + + +ContextifyContext::~ContextifyContext() { + env()->RemoveCleanupHook(CleanupHook, this); +} + + +void ContextifyContext::CleanupHook(void* arg) { + ContextifyContext* self = static_cast(arg); + self->context_.Reset(); + delete self; } diff --git a/src/node_contextify.h b/src/node_contextify.h index d04bf9ea28efb2..288b51ef56ac11 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -22,6 +22,8 @@ class ContextifyContext { ContextifyContext(Environment* env, v8::Local sandbox_obj, const ContextOptions& options); + ~ContextifyContext(); + static void CleanupHook(void* arg); v8::MaybeLocal CreateDataWrapper(Environment* env); v8::MaybeLocal CreateV8Context(Environment* env,