diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 1c9d9beb3d..c76b99d25c 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -66,11 +66,13 @@ class ContextifyContext { , sandbox_(env->isolate(), sandbox) , context_(env->isolate(), CreateV8Context(env)) , proxy_global_(env->isolate(), context()->Global()) - // Wait for both sandbox_'s and proxy_global_'s death - , references_(2) { - sandbox_.MakeWeak(this, SandboxFreeCallback); + // Wait for sandbox_, proxy_global_, and context_ to die + , references_(3) { + sandbox_.MakeWeak(this, WeakCallback); sandbox_.MarkIndependent(); - proxy_global_.MakeWeak(this, SandboxFreeCallback); + context_.MakeWeak(this, WeakCallback); + context_.MarkIndependent(); + proxy_global_.MakeWeak(this, WeakCallback); proxy_global_.MarkIndependent(); } @@ -175,9 +177,11 @@ class ContextifyContext { } - static void SandboxFreeCallback(Isolate* isolate, - Persistent* target, - ContextifyContext* context) { + template + static void WeakCallback(Isolate* isolate, + Persistent* target, + ContextifyContext* context) { + target->ClearWeak(); if (--context->references_ == 0) delete context; } diff --git a/test/pummel/test-vm-memleak.js b/test/pummel/test-vm-memleak.js index e0b087d81b..2abedc297b 100644 --- a/test/pummel/test-vm-memleak.js +++ b/test/pummel/test-vm-memleak.js @@ -38,9 +38,16 @@ var interval = setInterval(function() { if (Date.now() - start > 5 * 1000) { // wait 10 seconds. clearInterval(interval); + + testContextLeak(); } }, 1); +function testContextLeak() { + for (var i = 0; i < 1000; i++) + require('vm').createContext({}); +} + process.on('exit', function() { console.error('max mem: %dmb', Math.round(maxMem / (1024 * 1024))); // make sure we stay below 100mb