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. Problem with managing thread
Forum Updated to NodeBB v4.3 + New Features

Problem with managing thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthread
7 Posts 2 Posters 1.6k Views
  • 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.
  • M Offline
    M Offline
    michalt38
    wrote on 28 Oct 2016, 17:34 last edited by michalt38
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 28 Oct 2016, 18:22 last edited by VRonin
      #2
      • worker = new FileUploaderWorker(path); overwrites the old worker if you call this twice the previous worker will be leaking
      • worker->m_running = false; Make 100% sure m_running is of type std::atomic_bool
      • Probably it's just because the code you posted is just an example but I see no way emit finished(); is reached in FileUploaderWorker::process()

      This however probably does not help you with your main problem. so, can you show us where you call FileUploader::uploadFile from?

      The main suspect here is that you call connect on the same signal slot pair so try adding Qt::UniqueConnection to your connect statements and see if that fixes it. if it does you know where the problem is

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • M Offline
        M Offline
        michalt38
        wrote on 28 Oct 2016, 18:58 last edited by michalt38
        #3

        Adding Qt::UniqueConnection did not help. FileUploader::uploadFile is called from slot after clicking a button on a MainWindow:

        void MainWindow::uploadFile()
        {
            QUrl url = QFileDialog::getOpenFileUrl(this, "Open file", QUrl(""), "Text file (*.txt) (*txt)");
            QString path = url.path().remove(0, 1);
        
            if(path.isEmpty()) return;
        
            uploader->uploadFile(*this, path);
        
        }
        

        I tried add at the beginning of FileUploader::uploadFile:

        if(worker) delete worker;
        

        but then programm crashes :D m_running is of type std::atomic_bool. It was just an example but there is a break in while so emit finished can be reached in FileUploaderWorker::process().

        V 1 Reply Last reply 28 Oct 2016, 19:18
        0
        • M michalt38
          28 Oct 2016, 18:58

          Adding Qt::UniqueConnection did not help. FileUploader::uploadFile is called from slot after clicking a button on a MainWindow:

          void MainWindow::uploadFile()
          {
              QUrl url = QFileDialog::getOpenFileUrl(this, "Open file", QUrl(""), "Text file (*.txt) (*txt)");
              QString path = url.path().remove(0, 1);
          
              if(path.isEmpty()) return;
          
              uploader->uploadFile(*this, path);
          
          }
          

          I tried add at the beginning of FileUploader::uploadFile:

          if(worker) delete worker;
          

          but then programm crashes :D m_running is of type std::atomic_bool. It was just an example but there is a break in while so emit finished can be reached in FileUploaderWorker::process().

          V Offline
          V Offline
          VRonin
          wrote on 28 Oct 2016, 19:18 last edited by
          #4

          @michalt38 said in Problem with managing thread:

          I tried add at the beginning of FileUploader::uploadFile:
          if(worker) delete worker;

          but then program crashes

          It crashes because you forgot to assign NULL to worker in the constructor. anyway it should be something like:

          if(worker){
          QObject::disconnect(worker);
           worker->deleteLater();
          }
          

          in onError and onFinished you should set the worker back to NULL

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • M Offline
            M Offline
            michalt38
            wrote on 28 Oct 2016, 19:26 last edited by michalt38
            #5

            I have done what you wrote but I still have a problem beacouse when a programm comes to emit finished in FileUploaderWorker::process then the worker is still working and still sending some data to uC so when the programm emits finished it assigns NULL to worker and crashes. And it happens only when I run it the secound time or more. I noticed that it sends only every socound portion of the data, comes to emit finished and then tries to send some more data.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 29 Oct 2016, 07:07 last edited by
              #6

              That crashes because you have to put every worker-> inside a if(worker) to make sure you are not operatng on a null pointer.

              Can you tell me the exact sequence of operations that lead to the problem?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • M Offline
                M Offline
                michalt38
                wrote on 29 Oct 2016, 14:25 last edited by michalt38
                #7
                This post is deleted!
                1 Reply Last reply
                0

                1/7

                28 Oct 2016, 17:34

                • Login

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