Using embind + pthreads - deallocating shared_ptr from other thread #19299
Replies: 1 comment
-
Duplicate of #19311 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everybody,
I am facing a problem when allocating a C++ object from JS, which is handled by a
std::shared_ptr
and it is being deallocated from astd::thread
(reaching ref count 0).Let me introduce you with the most basic example to reproduce such problem:
lib.cpp
worker.js
index.html
And I would be building and serving the app with the commands:
Demo
This is the error when clicking the button and logic is triggered:
What I've tried so far
Using the derived type directly
I've noticed that if my C++ function consumes a
std::shared_ptr<Derived>
instead of astd::shared_ptr<Base>
then everything goes fine. The destructor is being called and there's no error.Somehow, the problem happens when the shared_ptr reference count reaches 0 and it needs to invoke the destructor of its base class, I guess.
And by the way, I can't change my application functions to receive the derived types.
Explicitly decreasing the ref count from the main thread
I tried to call
.reset()
to the shared pointer from inside a function passed toemscripten_sync_run_in_main_runtime_thread
. This is forcing the deallocation to be triggered from the main runtime thread, which has no problem in calling the destructor properly.But again this is something I would like to avoid since I could not be sure of how many places in my application I'd need to modify.
Beta Was this translation helpful? Give feedback.
All reactions