NulledPointer NulledPointer - 1 year ago 143
C++ Question

QTimer::SingleShot fired after object is deleted

//Example class
class A : pubic QObject
void fun() {
Timer::SingleShot(10, timerSlot); //rough code
public slot:
void timerSlot();

auto a = SharedPointer<A>(new A);
a->reset(); //a deleted

In this case after a is deleted and timer is fired, would it execute timerSlot()? I'm getting a extremely rare crash and not sure if its because of something fishy in this logic.

Answer Source

Even if the timer fires, it won't trigger the slot. The docs of ~QObject state: All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. The only way you can trigger the A::timerSlot and delete A at the same time is if you use threads.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download