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,