Nominate our 2022 Qt Champions!

Problem with managing thread

  • This post is deleted!

    • 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

  • 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().

  • @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:


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

  • 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.

  • 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?

  • This post is deleted!

Log in to reply