Apr 15, 2020 03:21 PM
(This post was last modified: Apr 15, 2020 03:25 PM by zNinja.)
in my case each class should be able to inherit shared_from_this from base class called bree_object_base
Now I have an class which creates a thread in its constructor.
In thread function I call shared_from_this()
Very rare happen that it throws exception. std::bad_weak_ptr .
Btw I use c++11 for this project.
https://en.cppreference.com/w/cpp/memory..._from_this
I am sure there are some race condition here caused by std::enable_shared_from_this
I would like to know theory, what happen, why sometimes it throws and sometimes not?
Now I have an class which creates a thread in its constructor.
In thread function I call shared_from_this()
Very rare happen that it throws exception. std::bad_weak_ptr .
Btw I use c++11 for this project.
https://en.cppreference.com/w/cpp/memory..._from_this
Quote:It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr<T>. Otherwise the behavior is undefined (until C++17)std::bad_weak_ptr is thrown (by the shared_ptr constructor from a default-constructed weak_this) (since C++17).
I am sure there are some race condition here caused by std::enable_shared_from_this
I would like to know theory, what happen, why sometimes it throws and sometimes not?

