问题描述
以下内容会导致堆栈溢出吗?
// reference elsewhere
this->update();
void devices::Sprinkler::update(){
if(this->_state == devices::Sprinkler::State::ON) {
...
QTimer::singleShot(this->_updateFrequency,this,SLOT(update()));
}
}
我知道是不是
update() {
update(); // stack overflow
}
但是我不完全了解前者的表现。
解决方法
否,因为调用不是递归的,所以不会引起堆栈溢出。 QTimer::singleShot()
安排在稍后的时间执行调用 ,允许update()
退出并清理其堆栈框架,然后再次调用,从而重用了堆栈空间。