Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

class inheritance enable shared from this (exception bad weak ptr)

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
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?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Release] by SIM-5525056Jul 10, 2024 03:13 PM 6 707 Jul 10, 2024 03:13 PM
Last Post: SIM-5525056
[C] by lockheadApr 22, 2021 07:10 AM 0 146 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C] by LunarManureDec 10, 2020 05:21 PM 13 530 Dec 10, 2020 05:21 PM
Last Post: LunarManure