c – 删除对象后触发的QTimer :: SingleShot
作者:互联网
// Example class
class A : public 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
在这种情况下删除a并触发计时器后,它会执行timerSlot()吗?我得到了一次非常罕见的崩溃,并且不确定是不是因为这个逻辑中有些可疑.
解决方法:
即使计时器触发,也不会触发插槽. ~QObject状态的文档:All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue.如果你使用线程,你可以同时触发A :: timerSlot并删除A的唯一方法.
标签:c,qt,qt5,qtimer,qeventloop 来源: https://codeday.me/bug/20190722/1502730.html