Skip to content

Commit 5c1d595

Browse files
addaleaxtargos
authored andcommitted
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: #28631 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent 03de306 commit 5c1d595

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/node_contextify.cc

+13
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ ContextifyContext::ContextifyContext(
114114

115115
context_.Reset(env->isolate(), v8_context.ToLocalChecked());
116116
context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter);
117+
env->AddCleanupHook(CleanupHook, this);
118+
}
119+
120+
121+
ContextifyContext::~ContextifyContext() {
122+
env()->RemoveCleanupHook(CleanupHook, this);
123+
}
124+
125+
126+
void ContextifyContext::CleanupHook(void* arg) {
127+
ContextifyContext* self = static_cast<ContextifyContext*>(arg);
128+
self->context_.Reset();
129+
delete self;
117130
}
118131

119132

src/node_contextify.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ContextifyContext {
2222
ContextifyContext(Environment* env,
2323
v8::Local<v8::Object> sandbox_obj,
2424
const ContextOptions& options);
25+
~ContextifyContext();
26+
static void CleanupHook(void* arg);
2527

2628
v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env);
2729
v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env,

0 commit comments

Comments
 (0)