How can I delete an old v8::Context?

127 views
Skip to first unread message

J

unread,
Aug 6, 2021, 8:41:42 AM8/6/21
to v8-users
Using embedded v8, I'm trying to clear the current variables and functions. 

I've found that in order to save having to recreate the isolate, which can be quite slow, I can just create a new context from the isolate. However, the old context still exists in memory and its not needed.

How can I delete an old context now that I don't need it?

Example code snippet:

```
// Creates a new execution environment containing the built-in
// functions.
v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
  // Create a template for the global object.
  v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
  // Bind the global 'print' function to the C++ Print callback.
  global->Set(isolate, "print", v8::FunctionTemplate::New(isolate, Print));
  // Bind the global 'read' function to the C++ Read callback.
  global->Set(isolate, "read", v8::FunctionTemplate::New(isolate, Read));
  // Bind the global 'load' function to the C++ Load callback.
  global->Set(isolate, "load", v8::FunctionTemplate::New(isolate, Load));
  // Bind the 'quit' function
  global->Set(isolate, "quit", v8::FunctionTemplate::New(isolate, Quit));
  // Bind the 'version' function
  global->Set(isolate, "version", v8::FunctionTemplate::New(isolate, Version));
  return v8::Context::New(isolate, NULL, global);
}
```

```
    v8::Local<v8::Context> context = CreateShellContext(isolate);
    if (context.IsEmpty()) {
      fprintf(stderr, "Error creating context\n");
      return 1;
    }
    if (run_shell) RunShell(context, platform.get());
```


Ben Noordhuis

unread,
Aug 7, 2021, 6:53:32 AM8/7/21
to v8-users
This question comes up every six months or so. If you search the
archives, you can probably find a longer discussion, but the short
answer is that you don't, it gets garbage collected when there are no
more references to it.
Reply all
Reply to author
Forward
0 new messages