Commit a26fa8dc authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Upon destruction, sigslot signals now wait for the signal_manager's signal...

Upon destruction, sigslot signals now wait for the signal_manager's signal queue to be flushed. This avoids crashed due to the signal_manager processing dangling signals, which have been deleted while being in the queue.
parent cbd7bcc6
......@@ -98,6 +98,36 @@ namespace sigslot {
_handlingMode = mode;
}
void signal_manager::waitForSignalQueueFlushed() {
// nothing to wait for if current thread is signal_manager's thread.
if (isCurrentThreadSignalManagerThread())
return;
// nothing to wait for if the signal queue is already empty.
if (_signalQueue.empty())
return;
class SIGSLOT_API flushed_signal : public _signal_handle_base {
public:
flushed_signal(bool* ptr)
: _ptr(ptr)
{}
~flushed_signal() {}
virtual void processSignal() const {
*_ptr = true;
}
private:
bool* _ptr;
};
bool signalVariable = false;
this->queueSignalImpl(new flushed_signal(&signalVariable));
while (! signalVariable)
std::this_thread::yield(); // so we do some busy waiting here - hopefully not too long...
}
const std::string signal_manager::loggerCat_;
......
......@@ -346,6 +346,13 @@ namespace sigslot {
/// \see Runnable:run
virtual void run();
/**
* Halts the calling thread until the current signal queue has been flushed.
* Flushing means that all signals currently in the signal queue have been fully handled.
*/
void waitForSignalQueueFlushed();
private:
/// Private constructor only for singleton
signal_manager();
......@@ -444,6 +451,11 @@ namespace sigslot {
class SIGSLOT_API _signal_base
{
public:
~_signal_base() {
if (signal_manager::isInited())
signal_manager::getRef().waitForSignalQueueFlushed();
}
virtual void slot_disconnect(has_slots* pslot) = 0;
virtual void slot_duplicate(has_slots const* poldslot, has_slots* pnewslot) = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment