src: fix MakeCallback() handle leak
authorBen Noordhuis <info@bnoordhuis.nl>
Sat, 16 Nov 2013 19:05:42 +0000 (20:05 +0100)
committerTimothy J Fontaine <tjfontaine@gmail.com>
Wed, 5 Feb 2014 19:49:01 +0000 (11:49 -0800)
Create a new HandleScope before looking up the object context with
v8::Object::CreationContext(), else we leak the Local<Context> into
the current HandleScope.

That's relatively harmless unless the HandleScope is long-lived and
MakeCallback() is called a lot.  In a scenario like that, we may end
up leaking a lot of memory.

What is unfortunate about this change is that we're trying hard to
eradicate the node_isolate global.  Longer term, we will probably have
to change the MakeCallback() prototype to one that requires an explicit
v8::Isolate* argument.

src/node.cc

index cd0c32f..18d23e2 100644 (file)
@@ -1150,10 +1150,10 @@ Handle<Value> MakeCallback(Handle<Object> recv,
                            const char* method,
                            int argc,
                            Handle<Value> argv[]) {
+  HandleScope handle_scope(node_isolate);  // FIXME(bnoordhuis) Isolate-ify.
   Local<Context> context = recv->CreationContext();
   Environment* env = Environment::GetCurrent(context);
   Context::Scope context_scope(context);
-  HandleScope handle_scope(env->isolate());
   return handle_scope.Close(MakeCallback(env, recv, method, argc, argv));
 }
 
@@ -1162,10 +1162,10 @@ Handle<Value> MakeCallback(Handle<Object> recv,
                            Handle<String> symbol,
                            int argc,
                            Handle<Value> argv[]) {
+  HandleScope handle_scope(node_isolate);  // FIXME(bnoordhuis) Isolate-ify.
   Local<Context> context = recv->CreationContext();
   Environment* env = Environment::GetCurrent(context);
   Context::Scope context_scope(context);
-  HandleScope handle_scope(env->isolate());
   return handle_scope.Close(MakeCallback(env, recv, symbol, argc, argv));
 }
 
@@ -1174,10 +1174,10 @@ Handle<Value> MakeCallback(Handle<Object> recv,
                            Handle<Function> callback,
                            int argc,
                            Handle<Value> argv[]) {
+  HandleScope handle_scope(node_isolate);  // FIXME(bnoordhuis) Isolate-ify.
   Local<Context> context = recv->CreationContext();
   Environment* env = Environment::GetCurrent(context);
   Context::Scope context_scope(context);
-  HandleScope handle_scope(env->isolate());
   return handle_scope.Close(
       MakeCallback(env, recv.As<Value>(), callback, argc, argv));
 }