NulledPointer NulledPointer - 17 days ago 5
C++ Question

QTimer::SingleShot fired after object is deleted

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

auto a = SharedPointer<A>(new A);
a->fun();
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

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.

Comments