![]() |
|
[C] class inheritance enable shared from this (exception bad weak ptr) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] class inheritance enable shared from this (exception bad weak ptr) (/showthread.php?tid=6067465) |
class inheritance enable shared from this (exception bad weak ptr) - zNinja - Apr 15, 2020 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/enable_shared_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? |