Search code examples
c++qtstack-overflow

QT -> timer loop, would this cause a stack overflow


Would the following create a stack overflow?

// reference elsewhere
this->update();


void devices::Sprinkler::update(){
    if(this->_state == devices::Sprinkler::State::ON) {
        ...
        QTimer::singleShot(this->_updateFrequency, this, SLOT(update()));
    }
}

I know that if it were

update() {
    update(); // stack overflow 
}

but I don't fullly understand how the former would behave.


Solution

  • No, it does not cause a stack overflow, because the call is not recursive. QTimer::singleShot() schedules a call for execution at a later time, allowing update() to exit and clean up its stack frame before it is called again, thus reusing stack space.