简体   繁体   中英

QThread: Destroyed while thread is still running and QMutex destroyed

I am trying to add multiple threads to my Qt application but right when it executes this thread the program just crashes and i get an error of

QThread: Destroyed while thread is still running

QMutex: destroying locked mutex

I understand the error message i just dont know how i can fix it. My code is below.

Header

class Worker : public QObject
{
    Q_OBJECT
private slots:
    void onTimeout()
    {
        qDebug()<<"Worker::onTimeout get called from?: "<<QThread::currentThreadId();
    }
};

class Thread : public QThread
{
    Q_OBJECT

private:
    void run()
    {
        qDebug()<<"From work thread: "<<currentThreadId();
        QTimer timer;
        Worker worker;
        connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
        timer.start(1000);
        exec();
    }
};

login.cpp

    void Login::on_pushButton_clicked()
{
    QSqlQuery query;
    QString Username = ui->Username_lineEdit->text();
    QString Password = ui->Password_lineEdit->text();
    query.prepare("SELECT * FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
    if(!query.exec())
    {
        qDebug() << "SQL QUERY Login:" << query.executedQuery();
        qDebug() << "SQL ERROR Login:" << query.lastError();
    }
    else if(!query.first())
    {
        tries++;
        int x = 10 - tries;
        ui->label->setText("Incorrect Username or Password " + QString::number(x) + " tries until timeout");
    }
    else
    {
        QSqlQuery Account_Type_Query("SELECT Account_Type FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
        while(Account_Type_Query.next())
        {
            Account_Type = Account_Type_Query.value(0).toInt();
        }
        tries = 0;
        static Home *home = new Home;
        home->show();
        close();
    }
    if(tries == 10)
    {
        Thread t;
        t.start();
        ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
        ui->pushButton->hide();
        QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
        tries = 0;
        ui->label->setText("");
    }

What is the correct way i can fix this issue. All help is much appreciated.

Thank you

Update: Tried class Thread : public QThread { Q_OBJECT

private:
    void run()
    {
        while(QThread::wait())
        {
        qDebug()<<"From work thread: "<<currentThreadId();
        QTimer timer;
        Worker worker;
        connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
        timer.start(1000);
        exec();
        }
        QThread::quit();
    }
};

but still receiving the same error

Inheriting from QThread is one issue here and I suspect that the Worker object does not have the thread affinity you think it has and is running on the main thread.

The reason for the crash is that you create your Thread instance on the stack,

if(tries == 10)
{
    Thread t; // NOTE THIS IS ON THE STACK
    t.start();
    ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
    ui->pushButton->hide();
    QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
    tries = 0;
    ui->label->setText("");
}

The Thread instance is being destroyed when it goes out of scope.

Regardless, I strongly suggest following the advice of @Thomas and adhere to the way in which Maya uses threads, without the need for inheriting QThread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM