Hi

I've some troubles with threading. In my application I've separated the GUI and a streaming/processing part into 2 threads. The streaming thread is created like the example in the QThread Class documentation. For a "single shot" that works great (starting and stopping the streaming thread). But if I re-click the streaming button the application crashes. There is no difference if I call the destructor for my background-worker class and recreate a complete new instance, or if I only re-call the thread start function.

Where is my problem?

Here are the specific parts:
Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. backgroundWorker = new workerThread;
  6. //Setup callback for cleanup when it finishes
  7. connect(backgroundWorker, SIGNAL(finished()), backgroundWorker, SLOT(deleteLater()));
  8. connect(backgroundWorker, SIGNAL(FrameReady()), this, SLOT(update_detection_lb()));
  9.  
  10. connect(ui->StartStopStreaming_pb, SIGNAL(clicked()), this, SLOT(Streaming()));
  11.  
  12. // Set gui labels
  13. ui->Gain_lb->setText(QString("%1 dB").arg(backgroundWorker->gain));
  14. ui->fc_lb->setText(QString("%1 MHz").arg(backgroundWorker->freq / 1e6));
  15. ui->adr_lb->setText(QString::fromStdString(backgroundWorker->addr));
  16. }
  17.  
  18. // Streaming button
  19. void MainWindow::Streaming()
  20. {
  21. if(ui->StartStopStreaming_pb->text() == "Start streaming")
  22. {
  23. ui->StartStopStreaming_pb->setText("Stop streaming");
  24.  
  25. std::cout << "Start streaming in background thread" << std::endl;
  26.  
  27. if(backgroundWorker)
  28. {
  29. backgroundWorker->m_abort = false;
  30. backgroundWorker->start(QThread::HighestPriority);
  31. }
  32. }
  33. else{
  34. ui->StartStopStreaming_pb->setText("Start streaming");
  35.  
  36. std::cout << "Stop streaming and terminate background thread" << std::endl;
  37.  
  38. backgroundWorker->m_abort = true;
  39. if(!backgroundWorker->wait(5000))
  40. {
  41. std::cout << "Thread deadlock detected, bad things may happen!" << std::endl;
  42.  
  43. backgroundWorker->terminate();
  44. backgroundWorker->wait();
  45. }
  46. }
  47. }
  48.  
  49. void workerThread::run()
  50. {
  51. std::cout << "Thread running" << std::endl;
  52.  
  53. while(m_abort == false)
  54. {
  55. // Do some streaming stuff
  56. }
  57.  
  58. std::cout << "Thread terminated" << std::endl;
  59. emit finished();
  60. }
To copy to clipboard, switch view to plain text mode 

Best regards,
P51D