C++ QThread:线程仍在运行时被销毁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24968400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:51:14  来源:igfitidea点击:

QThread: Destroyed while thread is still running?

c++qtsignals-slotsqthreadqwt

提问by The Man

I would like to start my QThreadwhen I push on button Run. But the compiler outputs following error:

QThread当我按下按钮Run时,我想开始我的。但是编译器输出以下错误:

QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cp.

I don't know what is wrong with my code.

我不知道我的代码有什么问题。

Any help would be appreciated.

任何帮助,将不胜感激。

Here is my code:

这是我的代码:

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;
}

回答by Erik

As the error message states: QThread: Destroyed while thread is still running. You are creating your SamplingThreadobject inside the MainWindow::startmethod but it goes out of scope (i.e. is destroyed) when that method terminates. There are two easy ways that I see:

正如错误消息所述:QThread: Destroyed while thread is still running. 您正在方法SamplingThread内部创建对象,MainWindow::start但是当该方法终止时它超出范围(即被销毁)。我看到有两种简单的方法:

  1. You make your SamplingThreada member of your MainWindowso its lifetime is the same as for the MainWindowinstance
  2. You use a pointer, i.e. you create the SamplingThreadusing

    SamplingThread *samplingThread = new SamplingThread;

  1. 您使您成为您SamplingThread的成员,MainWindow因此其生命周期与MainWindow实例相同
  2. 您使用指针,即您创建SamplingThread使用

    SamplingThread *samplingThread = new SamplingThread;

Does this help?

这有帮助吗?

Edit: to illustrate the two cases, a very crude example to show the two cases

编辑:为了说明这两种情况,一个非常粗略的例子来展示这两种情况

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

回答by Marek R

This is basics of C++! You are creating local object of QThreadon stack not on heap, so it gets destroys immediately when you leave method MainWindow::start.

这是C++的基础!您正在QThread堆栈上而不是在堆上创建本地对象,因此当您离开 method 时它会立即销毁MainWindow::start

It should be done like that:

应该这样做:

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

回答by Ulrich Eckhardt

There are two different "threads" involved: One is the actual thread, the other is the C++ object representing it (and to be correct, there is another thread from which this code is started in the first place).

涉及两个不同的“线程”:一个是实际线程,另一个是表示它的 C++ 对象(正确地说,还有另一个线程,该代码首先从该线程开始)。

The error just says that the thread is still running at the point where the C++ object representing it is destroyed. In your code, the reason is that the QThread instance is local to start(). Maybe you want to store the QThread in a member.

该错误只是表明线程仍在表示它的 C++ 对象被销毁的点上运行。在您的代码中,原因是 QThread 实例是start(). 也许您想将 QThread 存储在成员中。