How to properly dispose an isolate

379 views
Skip to first unread message

Darin Dimitrov

unread,
Dec 5, 2019, 8:55:37 AM12/5/19
to v8-users
Hello,

I am embedding V8 in my C++ application and I need to create multiple isolates from different background threads.

Here's my workflow:

1. Create an isolate in the main thread -> this must be a long living isolate (for the entire application lifetime)
2. Spawn multiple short living background threads.
3. Inside each background thread, create a new isolate using Isolate::New.
4. At the end of the thread, call isolate->Dispose.

My question is about properly disposing the short living isolates. I have noticed that if I call the isolate->Dispose() as a final instruction inside the background thread, chances are that subsequent calls to Isolate::New from other threads will return a pointer to an already disposed isolate. Ss a result of this reuse, I get heap-use-after free errors when I try to perform some operations on this isolate.

On the other hand, if I don't call isolate->Dispose() then each call to Isolate::New will return a fresh new pointer, but the memory claimed by this short living isolates will never be reclaimed and is leaking.

So what is the proper way to dispose the isolates? Should the Dispose call be made from the background thread in which the isolate was created, or I need to dispose it from the main thread (in which I have entered the main isolate). I have tried both approaches without luck.

Ben Noordhuis

unread,
Dec 6, 2019, 4:48:02 AM12/6/19
to v8-users
isolate->Dispose() is the right method to call. I don't believe it's
necessary to call it from the same thread (but neither does it hurt)
as long as the isolate isn't in use by another thread, i.e., hasn't
been Enter()'ed.

Use-after-free errors sound fishy. Isolate::New() heap-allocates the
isolate. Are you using a custom allocator?
Reply all
Reply to author
Forward
0 new messages