代码之家  ›  专栏  ›  技术社区  ›  John

QT->timer循环,这会导致堆栈溢出吗

  •  0
  • John  · 技术社区  · 4 年前

    下面的操作会造成堆栈溢出吗?

    // 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 
    }
    

    1 回复  |  直到 4 年前
        1
  •  2
  •   Remy Lebeau    4 年前

    不,它不会导致堆栈溢出,因为调用不是递归的。 QTimer::singleShot() 安排电话 以后再执行 ,允许 update() 在再次调用它之前退出并清理它的堆栈帧,从而重用堆栈空间。