Problem with managing thread
-
wrote on 28 Oct 2016, 17:34 last edited by michalt38This post is deleted!
-
wrote on 28 Oct 2016, 18:22 last edited by VRonin
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 -
wrote on 28 Oct 2016, 18:58 last edited by michalt38
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().
wrote on 28 Oct 2016, 19:18 last edited by@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
-
wrote on 28 Oct 2016, 19:26 last edited by michalt38
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.
-
wrote on 29 Oct 2016, 07:07 last edited by
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?
-
wrote on 29 Oct 2016, 14:25 last edited by michalt38This post is deleted!
7/7