簡體   English   中英

QThread:在線程仍在運行並且銷毀QMutex時銷毀

[英]QThread: Destroyed while thread is still running and QMutex destroyed

我正在嘗試將多個線程添加到我的Qt應用程序中,但是恰好在執行此線程時,程序崩潰了,並且出現錯誤

QThread:在線程仍在運行時被銷毀

QMutex:銷毀鎖定的互斥鎖

我了解錯誤消息,但我不知道該如何解決。 我的代碼如下。

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("");
    }

我可以解決此問題的正確方法是什么。 非常感謝所有幫助。

謝謝

更新:嘗試過的類線程:公共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();
    }
};

但仍然收到相同的錯誤

從QThread繼承是這里的一個問題,我懷疑Worker對象沒有您認為具有的線程親和力並且正在主線程上運行。

崩潰的原因是您在堆棧上創建了Thread實例,

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("");
}

線程實例超出范圍時將被銷毀。

無論如何,我強烈建議遵循@Thomas的建議,並堅持Maya使用線程的方式,而無需繼承QThread。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM