On Thu, Dec 5, 2019 at 2:55 PM Darin Dimitrov <darin.dimit...@gmail.com> wrote:
>
> 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.

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?

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
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 v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/CAHQurc_tKSV22wa2a81rMDeQcSHSQY_roxoYd3f7BKNqH8R2HQ%40mail.gmail.com.

Reply via email to