var res = new N1qlQuery('SELECT * FROM `sample`');
throw 'error'; // purposefully crash.
void N1qlQuery(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::Isolate *isolate = v8::Isolate::GetCurrent(); v8::HandleScope handleScope(isolate);
v8::Local<v8::Name> query_name = v8::String::NewFromUtf8(isolate, "query"); v8::Local<v8::Value> empty_string = v8::String::NewFromUtf8(isolate, "");
v8::Local<v8::ObjectTemplate> obj = v8::ObjectTemplate::New();
obj->Set(query_name, args[0]); args.GetReturnValue().Set(obj->NewInstance());}
There is no memory leak when the script finishes execution without crashing.
int SendUpdate(std::string value, std::string meta, std::string doc_type) { v8::Locker locker(GetIsolate()); v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate());
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(GetIsolate(), context_); v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(GetIsolate());
v8::Handle<v8::Value> args[2]; if (doc_type.compare("json") == 0) { args[0] = v8::JSON::Parse(v8::String::NewFromUtf8(GetIsolate(), value.c_str())); } else { args[0] = v8::String::NewFromUtf8(GetIsolate(), value.c_str()); }
args[1] = v8::JSON::Parse(v8::String::NewFromUtf8(GetIsolate(), meta.c_str()));
if (try_catch.HasCaught()) { last_exception = ExceptionString(GetIsolate(), &try_catch); LOG(logError) << "Last exception: " << last_exception << '\n'; }
v8::Local<v8::Function> on_doc_update = v8::Local<v8::Function>::New(GetIsolate(), on_update_); on_doc_update->Call(context->Global(), 2, args);
if (try_catch.HasCaught()) { LOG(logDebug) << "Exception message: " << ExceptionString(GetIsolate(), &try_catch) << '\n'; return ON_UPDATE_CALL_FAIL; }
return SUCCESS;}
I don't see an actual memory leak in the code you posted but creating
a new ObjectTemplate for every query isn't very efficient.
Seems relevant enough to me if you like efficient programs
It's not an issue when you create the ObjectTemplate once but it does become an issue when you create the ObjectTemplate afresh every time.
I'm not a V8 maintainer so I can't answer this but to me it seems like
reasonable behavior. The idea of a template is to share behavior
across multiple instances.