'v8::Context memory leaks

I'm trying to develop nodejs module on C++ using v8.

Here is source code.

#include <node.h>

using namespace v8;

void TestContext1(const FunctionCallbackInfo<Value>& args) {

    Isolate* isolate = Isolate::New();
    {
        Isolate::Scope isolate_scope(isolate);
        V8::Initialize();
        Locker lock(isolate);

        HandleScope handle_scope(isolate);

        Local<Context> context = Context::New(isolate);
        Context::Scope context_scope(context);


        Handle<String> strExpr = Handle<String>::Cast(args[0]);
        Handle<Script> script;
        Handle<Value> res;

        TryCatch tryCatch;
        script = Script::Compile(strExpr);
        res = script->Run();

        if (tryCatch.HasCaught()) {
            res = tryCatch.Message()->Get();
        }

        args.GetReturnValue().Set(res);
    }
    isolate->Dispose();

}


void TestContext2(const FunctionCallbackInfo<Value>& args) {

    Isolate *isolate = args.GetIsolate();
    Isolate::Scope isolate_scope(isolate);
    HandleScope handle_scope(isolate);
    Local<Context> context = Context::New(isolate);
    Context::Scope context_scope(context);

    Handle<String> strExpr = Handle<String>::Cast(args[0]);
    Handle<Script> script;
    Handle<Value> res;

    TryCatch tryCatch;
    script = Script::Compile(strExpr);
    res = script->Run();

    if (tryCatch.HasCaught()) {
        res = tryCatch.Message()->Get();
    }

    args.GetReturnValue().Set(res);
}

void Init(Handle<Object> exports, Handle<Object> module) {
    NODE_SET_METHOD(exports, "TestContext1", TestContext1);
    NODE_SET_METHOD(exports, "TestContext2", TestContext2);
}

NODE_MODULE(addon, Init)

And then testing it with js code:

var addon = require('addon.node')
var i = 0;
setInterval(function () {
    msg = addon.TestContext1("process.exit()"); // or msg = addon.TestContext2("process.exit()");
    console.log(msg, i++, process.memoryUsage().heapUsed/1024/1024);
}, 2);

Every function have different problems:

TestContext1 works fine but accessing global context and shutting down process.

TestContext2 does not have access to global context but gives memory leaks.

All what I need - execute js script in isolated context without memory leaks.

node vm doesn't work because vm.runInNewContext() have same memory leaks.

Does anybody have some ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source