Skip to content

Commit

Permalink
src: add cleanup hook for ContextifyContext
Browse files Browse the repository at this point in the history
Otherwise there’s a memory leak left by the context when the Isolate
tears down without having run the weak callback.

PR-URL: #28631
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
addaleax authored and targos committed Jul 20, 2019
1 parent 03de306 commit 5c1d595
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
2 changes: 2 additions & 0 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5c1d595

Please sign in to comment.