今天报一个bug,自动关机提示界面已经提示,但是一直不关机,打印log看到关机命令已经下发了,但是就是不关机,这是什么原因呢?
经过同事的帮助,发现关机命令下发了很多次,并且下发的间隔很短,而我原本的设计想法是提示关机,等待两秒后下发关机命令,想必是这里出问题了,不应该下发很多次。查看log发现,电量查询定时器多次调用batteryTimerTimeOut的showAlertWgt,但一直没有调用showAlertWgt里面的延时关机的定时器,直到log的最后却有很多次调用。
[1970-01-01 08:06:13.503] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:15.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:17.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:19.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:21.506] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:23.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:25.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:27.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:29.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:31.503] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:33.502] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:35.502] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:37.569] [operator()]: send poweroff finished
[1970-01-01 08:06:37.641] [operator()]: send poweroff finished
[1970-01-01 08:06:37.735] [operator()]: send poweroff finished
[1970-01-01 08:06:37.810] [operator()]: send poweroff finished
[1970-01-01 08:06:37.892] [operator()]: send poweroff finished
[1970-01-01 08:06:37.968] [operator()]: send poweroff finished
代码如下
showAlertWgt(){
..........
alertHideTime->stop();
QMetaObject::Connection ret = connect(alertHideTime, &QTimer::timeout, myWgt,
[]() {
system("poweroff");
}
);
alertHideTime->start(2000);
.........
}
整个现象就是alertHideTime不被调用,原因是电池check的timer间隔是2s,而alertHideTime的间隔也是2s,在alertHideTime到期之前就被再一次调用showAlertWgt()的时候stop了,所以一直没有下发关机命令(当然,设计之初是没有这个bug的,因为一开始电池刷新的timer间隔是5s,也就不存在alertHideTime被stop的问题了,只是后来驱动同事叫我把间隔缩短,于是有这个问题了,并且那时候没有测出来);而后面为什么又会运行关机命令呢,原因是连接usb以后,系统有供电了,就不会再运行showAlertWgt(),也就不会去stop这个alertHideTime了,因此两秒后时间到了,就会执行关机命令,也就是看到的现象:关机提示语出现,一直不关机,插上usb,马上关机。
这个log中还有一个问题,关机命令被下发了很多次,原因是QTimer每次stop以后,就去connect了一个函数,也就是QTimer不能被多次connect,应该在init的时候就connect完成,后面就不要再设置了,要不然,之前connect的每个槽都会被执行。