tufao
1.3.0
An asynchronous web framework for C++ built on top of Qt
|
We say that a signal is safe when it's safe to delete the object in the slot connected to this signal.
This property implies that no member-function will access the attributes of the object after emit the signal (and won't schedule such accesses).
Consider the following code:
In the previous code, someSignal is an unsafe signal, because there is accesses to the internal state of the object after it emits the signal. We can refactor the previous code to make someSignal safe by reordering. See the final version, where someSignal is safe:
Why do you wanna a safe signal? They are safer and you need to worry less about invalid access. Consider you have an object O – allocated on the heap – of type SomeObject and a method M connected to the signal someSignal, then the following code is valid:
If you have an unsafe signal, your only choice is to use QObject::deleteLater.
Sometimes, it's too complicated to create a safe signal. Consider you receive a message via the network and some method parse this message. Consider the method should emit signals each time a new message is ready, something like the code below:
The above code uses an unsafe signal. Sure you could convert the code to use safe signals, but the overhead of performance and responsiveness created wouldn't be worth.
Another example of an unsafe signal:
Every signal in Tufão is safe, unless explicitly stated the opposite.
Some alternative ways of deleting an object in code triggered by an unsafe signal are: