[英]QThread: Destroyed while thread is still running?
当我按下按钮Run时,我想启动我的QThread
。 但编译器输出以下错误:
QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cp.
我不知道我的代码有什么问题。
任何帮助,将不胜感激。
这是我的代码:
SamplingThread::SamplingThread( QObject *parent):
QwtSamplingThread( parent ),
d_frequency( 5.0 )
{
init();
}
MainWindow::MainWindow( QWidget *parent ):
QMainWindow( parent )
{.......
.....
run= new QPushButton ("Run",this);
stop= new QPushButton("Stop",this);
connect(run, SIGNAL(clicked()),this, SLOT (start()));
}
MainWindow::start
{
SamplingThread samplingThread;
samplingThread.setFrequency( frequency() );
samplingThread.start();
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
MainWindow window;
window.resize( 700, 400 );
window.show();
bool ok = app.exec();
return ok;
}
正如错误消息所述: QThread: Destroyed while thread is still running
。 您正在MainWindow::start
方法中创建SamplingThread
对象,但是当该方法终止时,它会超出范围(即被销毁)。 我看到了两种简单的方法:
SamplingThread
成为MainWindow
的成员,因此其生命周期与MainWindow
实例的生命周期相同 您使用指针,即使用创建SamplingThread
SamplingThread *samplingThread = new SamplingThread;
这有帮助吗?
编辑:为了说明这两种情况,一个非常粗略的例子来说明这两种情况
#include <iostream>
#include <QApplication>
#include <QThread>
class Dummy
{
public:
Dummy();
void start();
private:
QThread a;
};
Dummy::Dummy() :
a()
{
}
void Dummy::start()
{
a.start();
QThread *b = new QThread;
b->start();
if( a.isRunning() ) {
std::cout << "Thread a is running" << std::endl;
}
if( b->isRunning() ) {
std::cout << "Thread b is running" << std::endl;
}
}
int main(int argc, char** argv)
{
QApplication app(argc,argv);
Dummy d;
d.start();
return app.exec();
}
这是C ++的基础知识! 您正在堆栈上创建QThread
本地对象而不是在堆上,因此当您离开方法MainWindow::start
时它会立即销毁。
应该这样做:
MainWindow::MainWindow( QWidget *parent ):
QMainWindow( parent )
{
...
samplingThread = SamplingThread(this);
samplingThread->setFrequency( frequency() );
run= new QPushButton ("Run",this);
stop= new QPushButton("Stop",this);
connect(run, SIGNAL(clicked()), samplingThread, SLOT(start()));
}
MainWindow::~MainWindow() {
samplingThread->waitFor(5000);
}
涉及两个不同的“线程”:一个是实际线程,另一个是表示它的C ++对象(并且是正确的,还有另一个线程,首先从该线程启动此代码)。
该错误只表示该线程仍然在代表它的C ++对象被销毁的位置运行。 在您的代码中,原因是QThread实例是start()
本地实例。 也许你想将QThread存储在一个成员中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.