Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread: Destroyed while thread is still running
Forum Updated to NodeBB v4.3 + New Features

QThread: Destroyed while thread is still running

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 791 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wang xiaohu
    wrote on last edited by Wang xiaohu
    #1

    When I use Qt multithreading, I encounter this problem: "QThread: Destroyed while thread is still running".
    QThread.png

    The MainWindow.h :

    class MainWindow : public QMainWindow 
    {
    .....
    private:
        QThread TerraThread;
        TerraWorker* terraWorker = NULL;
    }
    

    The MainWindow.cpp :

    MainWindow::MainWindow()
    {
        terraWorker = new TerraWorker;
        terraWorker->moveToThread(&TerraThread);
        connect(terraWorker, &QObject::destroyed, &TerraThread, &QThread::quit);
        connect(&TerraThread, &QThread::finished, terraWorker, &QObject::deleteLater);
        connect(&TerraThread, &QThread::started, terraWorker, &TerraWorker::init);
        TerraThread.start();
    }
    

    The terraWoker.h :

    #pragma once
    #include <QObject>
    #include "terrasinkevent.h"
    
    class TerraWorker :   public QObject
    {
    	Q_OBJECT
    
    public:
    	//TerraWorker();
    	//~TerraWorker();
    
    public slots:
    	void init();
    private:
    
    signals:
    
    private:
    	TerraSinkEvent* m_pTerraSink = NULL;
    	ISGWorld71Ptr m_pISGWorld = NULL;
    };
    
    

    The terraWorker.cpp :

    #include "terraworker.h"
    #include <QDebug>
    #include <QtCore/QThread> 
    
     void TerraWorker::init()
     {
         CoInitialize(NULL);
         
         //TerraSinkEvent* m_pTerraSink = new TerraSinkEvent();
         //ISGWorld71Ptr m_pISGWorld = NULL;
         m_pTerraSink = new TerraSinkEvent();
         m_pISGWorld.CreateInstance(CLSID_SGWorld71);
         if (m_pISGWorld != NULL)
         {
             HRESULT hr = m_pTerraSink->DispEventAdvise(m_pISGWorld);
             if (FAILED(hr))
             {
                 qDebug() << "DispEventAdvise Failed...";
             }
         }
         m_pISGWorld->Project->Open("C:\\Skyline\\TerraExplorer\\Beijing-15\\Beijing_15.FLY", FALSE, "", "");
         IPosition71Ptr pIPosition71 = m_pISGWorld->Creator->CreatePosition(116.2, 40.4, 1500, AltitudeTypeCode::ATC_TERRAIN_RELATIVE, 0, 0, 0, 0);
         m_pISGWorld->Navigate->FlyTo(_variant_t((IDispatch*)pIPosition71), ActionCode::AC_FLYTO);
         qDebug() << "TerraWorker Thread: " << QThread::currentThreadId();
     }
    

    The terrasinkevent.h :

    #pragma once
    
    #include <atlbase.h>
    #include <atlcom.h>
    #include <QDebug>
    
    #include "terraexplorerx.tlh"
    
    static _ATL_FUNC_INFO info = { CC_STDCALL, VT_EMPTY, 1, {VT_BOOL} };
    static _ATL_FUNC_INFO onLButtonDownInfo = { CC_STDCALL, VT_EMPTY, 3, {VT_I4, VT_I4, VT_I4 } };
    static _ATL_FUNC_INFO infoEmpty = { CC_STDCALL, VT_EMPTY, 0, {} };
    
    
        class  TerraSinkEvent : public IDispEventSimpleImpl
                    <1, TerraSinkEvent, &DIID__ISGWorld71Events>
    {
    public:
        TerraSinkEvent();
    
        ~TerraSinkEvent();
    
        STDMETHODIMP OnLButtonDown(long Flags, int X, int Y, VARIANT_BOOL* pbHandled)
        {
            qDebug() << "Jump into OnLButtonDown...";
            return S_OK;
        }
    
        STDMETHODIMP OnLoadFinished(VARIANT_BOOL bSuccess)
        {
            return S_OK;
        }
    
    
        BEGIN_SINK_MAP(TerraSinkEvent)
            SINK_ENTRY_INFO(1, DIID__ISGWorld71Events, 9, OnLButtonDown, &onLButtonDownInfo)
        END_SINK_MAP()
    
    };
    

    The * terrasinkevent.cpp* :

    #include "terrasinkevent.h"
    
    TerraSinkEvent::TerraSinkEvent()
    {
    
     }
    
    TerraSinkEvent::  ~TerraSinkEvent()
    {
     
    }
    
    

    I think I use the QThread in right way, but, why it arises "QThread: Destroyed while thread is still running", when I quit the "MainWindow"?

    When I comment these lines in MainWindow.cpp :

        //terraWorker->moveToThread(&TerraThread);
        //connect(terraWorker, &QObject::destroyed, &TerraThread, &QThread::quit);
        //connect(&TerraThread, &QThread::finished, terraWorker, &QObject::deleteLater);
        //connect(&TerraThread, &QThread::started, terraWorker, &TerraWorker::init);
        //TerraThread.start();
    

    And use :

        terraWorker = new TerraWorker;
        terraWorker->init();
    

    This doesn't arise this error, when I quit MainWindow.

    JKSHJ 1 Reply Last reply
    0
    • W Wang xiaohu

      When I use Qt multithreading, I encounter this problem: "QThread: Destroyed while thread is still running".
      QThread.png

      The MainWindow.h :

      class MainWindow : public QMainWindow 
      {
      .....
      private:
          QThread TerraThread;
          TerraWorker* terraWorker = NULL;
      }
      

      The MainWindow.cpp :

      MainWindow::MainWindow()
      {
          terraWorker = new TerraWorker;
          terraWorker->moveToThread(&TerraThread);
          connect(terraWorker, &QObject::destroyed, &TerraThread, &QThread::quit);
          connect(&TerraThread, &QThread::finished, terraWorker, &QObject::deleteLater);
          connect(&TerraThread, &QThread::started, terraWorker, &TerraWorker::init);
          TerraThread.start();
      }
      

      The terraWoker.h :

      #pragma once
      #include <QObject>
      #include "terrasinkevent.h"
      
      class TerraWorker :   public QObject
      {
      	Q_OBJECT
      
      public:
      	//TerraWorker();
      	//~TerraWorker();
      
      public slots:
      	void init();
      private:
      
      signals:
      
      private:
      	TerraSinkEvent* m_pTerraSink = NULL;
      	ISGWorld71Ptr m_pISGWorld = NULL;
      };
      
      

      The terraWorker.cpp :

      #include "terraworker.h"
      #include <QDebug>
      #include <QtCore/QThread> 
      
       void TerraWorker::init()
       {
           CoInitialize(NULL);
           
           //TerraSinkEvent* m_pTerraSink = new TerraSinkEvent();
           //ISGWorld71Ptr m_pISGWorld = NULL;
           m_pTerraSink = new TerraSinkEvent();
           m_pISGWorld.CreateInstance(CLSID_SGWorld71);
           if (m_pISGWorld != NULL)
           {
               HRESULT hr = m_pTerraSink->DispEventAdvise(m_pISGWorld);
               if (FAILED(hr))
               {
                   qDebug() << "DispEventAdvise Failed...";
               }
           }
           m_pISGWorld->Project->Open("C:\\Skyline\\TerraExplorer\\Beijing-15\\Beijing_15.FLY", FALSE, "", "");
           IPosition71Ptr pIPosition71 = m_pISGWorld->Creator->CreatePosition(116.2, 40.4, 1500, AltitudeTypeCode::ATC_TERRAIN_RELATIVE, 0, 0, 0, 0);
           m_pISGWorld->Navigate->FlyTo(_variant_t((IDispatch*)pIPosition71), ActionCode::AC_FLYTO);
           qDebug() << "TerraWorker Thread: " << QThread::currentThreadId();
       }
      

      The terrasinkevent.h :

      #pragma once
      
      #include <atlbase.h>
      #include <atlcom.h>
      #include <QDebug>
      
      #include "terraexplorerx.tlh"
      
      static _ATL_FUNC_INFO info = { CC_STDCALL, VT_EMPTY, 1, {VT_BOOL} };
      static _ATL_FUNC_INFO onLButtonDownInfo = { CC_STDCALL, VT_EMPTY, 3, {VT_I4, VT_I4, VT_I4 } };
      static _ATL_FUNC_INFO infoEmpty = { CC_STDCALL, VT_EMPTY, 0, {} };
      
      
          class  TerraSinkEvent : public IDispEventSimpleImpl
                      <1, TerraSinkEvent, &DIID__ISGWorld71Events>
      {
      public:
          TerraSinkEvent();
      
          ~TerraSinkEvent();
      
          STDMETHODIMP OnLButtonDown(long Flags, int X, int Y, VARIANT_BOOL* pbHandled)
          {
              qDebug() << "Jump into OnLButtonDown...";
              return S_OK;
          }
      
          STDMETHODIMP OnLoadFinished(VARIANT_BOOL bSuccess)
          {
              return S_OK;
          }
      
      
          BEGIN_SINK_MAP(TerraSinkEvent)
              SINK_ENTRY_INFO(1, DIID__ISGWorld71Events, 9, OnLButtonDown, &onLButtonDownInfo)
          END_SINK_MAP()
      
      };
      

      The * terrasinkevent.cpp* :

      #include "terrasinkevent.h"
      
      TerraSinkEvent::TerraSinkEvent()
      {
      
       }
      
      TerraSinkEvent::  ~TerraSinkEvent()
      {
       
      }
      
      

      I think I use the QThread in right way, but, why it arises "QThread: Destroyed while thread is still running", when I quit the "MainWindow"?

      When I comment these lines in MainWindow.cpp :

          //terraWorker->moveToThread(&TerraThread);
          //connect(terraWorker, &QObject::destroyed, &TerraThread, &QThread::quit);
          //connect(&TerraThread, &QThread::finished, terraWorker, &QObject::deleteLater);
          //connect(&TerraThread, &QThread::started, terraWorker, &TerraWorker::init);
          //TerraThread.start();
      

      And use :

          terraWorker = new TerraWorker;
          terraWorker->init();
      

      This doesn't arise this error, when I quit MainWindow.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Wang-xiaohu said in QThread: Destroyed while thread is still running:

      why it arises "QThread: Destroyed while thread is still running
      ", when I quit the "MainWindow"?

      Because you destroyed the QThread object when the thread was still running.

      You must wait until the thread stops. Call QThread::wait() in your MainWindow's destructor.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • W Offline
        W Offline
        Wang xiaohu
        wrote on last edited by
        #3

        thank you, I'll have a try.
        I thought it would be OK to use QThread::quit() function.

        JKSHJ 1 Reply Last reply
        0
        • W Wang xiaohu

          thank you, I'll have a try.
          I thought it would be OK to use QThread::quit() function.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Wang-xiaohu said in QThread: Destroyed while thread is still running:

          I thought it would be OK to use QThread::quit() function.

          QThread::quit() causes the thread to begin shutting down.

          QThread::wait() waits for the thread to finish shutting down.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved