File tree Expand file tree Collapse file tree 1 file changed +5
-5
lines changed
Expand file tree Collapse file tree 1 file changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -73,13 +73,13 @@ class TrackableSignal {
7373
7474 // This class can't hold the trackable's shared_ptr, because disconnect should be able to be called in the
7575 // the trackable's destructor. However, the trackable can not be destroyed when the slot is being called
76- // either. `track_foreign` is racey because the control block can be released from some thread without a mutex
77- // protecting it while another one is invoking the slot. Therefore we are storing a weak_ptr of the trackable
78- // and explicitly checking it in the slot invocation instead. The invocation is guaranteed to happen under an
79- // internal mutex so it's safe .
76+ // either. `track_foreign` is racey when one shared_ptr is tracked by multiple signals. Therefore we are storing
77+ // a weak_ptr of the trackable and using weak_ptr::lock() to atomically check existence and acquire a shared_ptr
78+ // during slot invocation. This guarantees to keep the trackable alive for the duration of the slot call and
79+ // avoids potential race conditions .
8080 connections->emplace (
8181 trackable.get (), signal_.connect ([slot, weakTrackable = std::weak_ptr (trackable)](Args&&... args) {
82- if (not weakTrackable.expired () )
82+ if (auto lifeExtender = weakTrackable.lock (); lifeExtender )
8383 std::invoke (slot, std::forward<Args...>(args)...);
8484 })
8585 );
You can’t perform that action at this time.
0 commit comments