Home > database >  Variable not updating in lambda slot
Variable not updating in lambda slot

Time:01-25

I need to run a function with arguments (Installer::install) on a QProcess::finished signal. I've decided to use a lambda as slot, resulting in this:

private:
    QProcess* unzip = new QProcess(this);`
void Installer::extract(QString source, QString destination) {
    QStringList args;
    args.append("x");
    args.append(source);
    args.append(QString("-o%1").arg(destination));
    connect(unzip, &QProcess::finished, this, [=](int code, QProcess::ExitStatus status) {
        if (code != 0 || status == QProcess::CrashExit) {
            qDebug() << "Installer - Error >> Exit code:" << code;
            qDebug() << "Installer - Error >> Status:" << status;
            emit error();
        }
        else
            install(destination   "/game.exe");
        });
    unzip->start("path\to\extractor\program.exe", args);
    if (!unzip->waitForStarted()) {
        qDebug() << "Installer - Error >> Extractor not started";
        qDebug() << "Installer - Error Detail >>" << unzip->error();
        emit error();
        return;
    }
    qDebug() << "Sot Installer - Updates - Extractor >> Extracting...";
}

Installer::extract function is called at the end of every extraction. Supposing we have four archives to extract, Installer::extract will be called four times, with four different destination.

For the first archive, all works well. From the second one, the destination variable inside the lambda slot has the value of the first destination while it should have the second destination value. Instead, the destination variable outside the slot (e.g. in the last args.append) has the right value. Why is this happening and how can I solve this?

CodePudding user response:

According to @Igor Tandetnik, connections were stacking up and so were the values of destination. Disconnecting all the connections of unzip before running Installer::extract again solved the problem.

  • Related