Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
vm: cleanup module memory leakage
Browse files Browse the repository at this point in the history
There are some paths here that led to dangling contexts. By being smarter with
handle management we can get rid of all the cleanup code and fix those issues.

This is a backport of commit 7063575.
  • Loading branch information
laverdet authored and bnoordhuis committed May 20, 2012
1 parent f19f980 commit 7865c5c
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/node_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,18 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
display_error = true;
}

Persistent<Context> context;
Handle<Context> context = Context::GetCurrent();

Local<Array> keys;
if (context_flag == newContext) {
// Create the new context
context = Context::New();
// Context::New returns a Persistent<Context>, but we only need it for this
// function. Here we grab a temporary handle to the new context, assign it
// to a local handle, and then dispose the persistent handle. This ensures
// that when this function exits the context will be disposed.
Persistent<Context> tmp = Context::New();
context = Local<Context>::New(tmp);
tmp.Dispose();

} else if (context_flag == userContext) {
// Use the passed in context
Expand All @@ -362,11 +368,10 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
context = nContext->GetV8Context();
}

Context::Scope context_scope(context);

// New and user context share code. DRY it up.
if (context_flag == userContext || context_flag == newContext) {
// Enter the context
context->Enter();

// Copy everything from the passed in sandbox (either the persistent
// context for runInContext(), or the sandbox arg to runInNewContext()).
CloneObject(args.This(), sandbox, context->Global()->GetPrototype());
Expand Down Expand Up @@ -408,11 +413,6 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
if (output_flag == returnResult) {
result = script->Run();
if (result.IsEmpty()) {
if (context_flag == newContext) {
context->DetachGlobal();
context->Exit();
context.Dispose();
}
return try_catch.ReThrow();
}
} else {
Expand All @@ -430,16 +430,6 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
CloneObject(args.This(), context->Global()->GetPrototype(), sandbox);
}

if (context_flag == newContext) {
// Clean up, clean up, everybody everywhere!
context->DetachGlobal();
context->Exit();
context.Dispose();
} else if (context_flag == userContext) {
// Exit the passed in context.
context->Exit();
}

return result == args.This() ? result : scope.Close(result);
}

Expand Down

0 comments on commit 7865c5c

Please sign in to comment.