Problem with managing thread
-
worker = new FileUploaderWorker(path);
overwrites the old worker if you call this twice the previous worker will be leakingworker->m_running = false;
Make 100% surem_running
is of typestd::atomic_bool
- Probably it's just because the code you posted is just an example but I see no way
emit finished();
is reached inFileUploaderWorker::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().
-
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:if(worker){ QObject::disconnect(worker); worker->deleteLater(); }
in
onError
andonFinished
you should set the worker back toNULL
-
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.