src: add cleanup hook for ContextifyContext

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 <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Anna Henningsen 2019-07-11 00:12:02 +02:00
parent 518ffc1256
commit 00464b5282
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 15 additions and 0 deletions

View File

@ -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<ContextifyContext*>(arg);
self->context_.Reset();
delete self;
}

View File

@ -22,6 +22,8 @@ class ContextifyContext {
ContextifyContext(Environment* env,
v8::Local<v8::Object> sandbox_obj,
const ContextOptions& options);
~ContextifyContext();
static void CleanupHook(void* arg);
v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env);
v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env,