打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
QThread: Destroyed while thread is still running | 错误123
简介:
在QT开发中使用QThread多线程,线程没正常释放导致错误: “QThread: Destroyed while thread is still running”。
环境:
系统: Microsoft Windows [版本 6.1.7601] (Windows 7 旗舰版64位)
QT:
Qt Creator 4.3.1
Based on Qt 5.9.0 (MSVC 2015, 32 bit)Built on May 28 2017 21:47:46
错误:
1
2
3
4
QThread: Destroyed while thread is still running
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

 
操作:
1. 继承QThread作为子线程:

类: parentThread001

1
parentThread001
parentthread001.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PARENTTHREAD001_H
#define PARENTTHREAD001_H
#include <QThread>
class parentThread001 : public QThread
{
    Q_OBJECT
public:
    explicit parentThread001(QObject *parent = Q_NULLPTR);
    ~parentThread001();
protected:
    void run();
};
#endif // PARENTTHREAD001_H

parentthread001.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "parentthread001.h"
#ifdef QT_DEBUG
#include <QDebug>
#endif
parentThread001::parentThread001(QObject *parent)
            :QThread(parent)
{
}
parentThread001::~parentThread001()
{
}
void parentThread001::run()
{
    while (true)
    {
#ifdef QT_DEBUG
        qDebug()<<"parentThread001::run: pid="<<QThread::currentThreadId()<<endl;
#endif
        //
        this->msleep(100);
    }
}

 
 2. 使用moveToThread方式作为子线程:
类: moveThread001
movethread001.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MOVETHREAD001_H
#define MOVETHREAD001_H
#include <QObject>
class  QThread;
class moveThread001 : public QObject
{
    Q_OBJECT
public:
    explicit moveThread001(QObject *parent = nullptr);
    ~moveThread001();
signals:
public slots:
    void onTest();
protected:
    QThread *m_td;
};
#endif // MOVETHREAD001_H

movethread001.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "movethread001.h"
#include <QThread>
#ifdef QT_DEBUG
#include <QDebug>
#endif
moveThread001::moveThread001(QObject *parent) : QObject(parent)
{
    this->m_td = new QThread();
    this->moveToThread(this->m_td);
    this->m_td->start();
}
moveThread001::~moveThread001()
{
    delete this->m_td;
}
void moveThread001::onTest()
{
#ifdef QT_DEBUG
    qDebug()<<"moveThread001::onTest: pid="<<QThread::currentThreadId()<<endl;
#endif
}

 
使用:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <QCoreApplication>
#include <QThread>
#include <QTimer>
#ifdef QT_DEBUG
#include <QDebug>
#endif
#include "parentthread001.h"
#include "movethread001.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //
#ifdef QT_DEBUG
    qDebug()<<"main: pid="<<QThread::currentThreadId()<<endl;
#endif
//-----------------------------------------------
#ifdef QT_DEBUG
    qDebug()<<"parentThread001: begin."<<endl;
#endif
    parentThread001 *pt = new parentThread001();
    pt->start();
    QThread::msleep(1000);
    delete pt;
#ifdef QT_DEBUG
    qDebug()<<"parentThread001: end."<<endl;
#endif
//-----------------------------------------------
#ifdef QT_DEBUG
    qDebug()<<"moveThread001: begin."<<endl;
#endif
    moveThread001 *mt = new moveThread001();
    QThread::msleep(1000);
    delete mt;
#ifdef QT_DEBUG
    qDebug()<<"moveThread001: end."<<endl;
#endif
    //
    return a.exec();
}

 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
"_DEBUG"与"DEBUG"的区别
在Qt程序中加入C文件
trace
Qt 的线程与事件循环
Qt的线程(两种QThread类的详细使用方式)
关于QT的movetothread用法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服