From a3bf3d10ef4b373870c6bb475f8fd5b6ede953cc Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Fri, 23 Aug 2013 21:33:16 -0400 Subject: [PATCH] vm: use MakeWeak to fix leaking contexts This is always something you should do when using `SetHiddenValue`, apparently. Fixes #6115. Thanks @tjfontaine for the tips. --- src/node_contextify.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 11bb979..3f47f52 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -35,6 +35,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::Integer; +using v8::Isolate; using v8::Local; using v8::None; using v8::Object; @@ -62,6 +63,8 @@ class ContextifyContext { Local v8_context = CreateV8Context(); context_.Reset(node_isolate, v8_context); proxy_global_.Reset(node_isolate, v8_context->Global()); + sandbox_.MakeWeak(this, SandboxFreeCallback); + sandbox_.MarkIndependent(); } @@ -127,10 +130,18 @@ class ContextifyContext { } Local sandbox = args[0].As(); - Local context = External::New(new ContextifyContext(sandbox)); + ContextifyContext* context = new ContextifyContext(sandbox); + Local hidden_context = External::New(context); Local hidden_name = FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden"); - sandbox->SetHiddenValue(hidden_name, context); + sandbox->SetHiddenValue(hidden_name, hidden_context); + } + + + static void SandboxFreeCallback(Isolate* isolate, + Persistent* target, + ContextifyContext* context) { + delete context; } -- 2.7.4