Thank you Ben.
Yes, there's a try_catch.HasCaught() before running the script in order to 
prevent the process from exiting.
Here's the code that will cause the script to run -
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;
}

Is it possible to reclaim the memory without shutting the VM down?


On Tuesday, 18 April 2017 12:42:58 UTC+5:30, Ben Noordhuis wrote:
>
> On Tue, Apr 18, 2017 at 7:32 AM, Gautham B A 
> <[email protected] <javascript:>> wrote: 
> > Hi all, 
> > 
> > I'm observing a memory leak when the script crashes at runtime. Here the 
> > JavaScript code (made to crash on purpose by throwing Exception). 
> > var res = new N1qlQuery('SELECT * FROM `sample`'); 
> > throw 'error'; // purposefully crash. 
> > 
> > The corresponding C++ code for N1qlQuery is as follows - 
> > 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()); 
> > } 
> > 
> > I believe obj->NewInstance() is causing the memory leak, because I've 
> > observed no memory leak when the last line in C++ was commented out, 
> even if 
> > the JavaScript crashed. 
> >> 
> >> There is no memory leak when the script finishes execution without 
> >> crashing. 
> > 
> > 
> > Could anyone please tell me the right way to expose a JavaScript class? 
> > (perhaps by avoiding a call to NewInstance() in C++). 
> > 
> > Thanks, 
> > --Gautham 
>
> You should post full code if you want an answer but if I had to guess, 
> it's that you have a `if (try_catch.HasCaught()) exit();` or the 
> equivalent thereof in your code somewhere - i.e., termination without 
> proper shutdown of the VM. 
>
> Aside: you can use `args.GetIsolate()` instead of 
> `Isolate::GetCurrent()`, it's a little faster, and you don't need a 
> HandleScope, API callbacks have one implicitly. 
>

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to