QProgressDialog or MainWindow with table Crashes when using with MultiThread
-
I have a QProgressDialog which tell me progress of my operation but the dialog crashes at setValue after reaching around 90%.
QProgressDialog* progressDialog // Member variable defined in MainWindow header file.// Source file
void startProcessing()
{
progressDialog = new QProgressDialog(this);
progressDialog ->setWindowModality(Qt::WindowModal);
progressDialog ->setWindowTitle("Calculation");
progressDialog ->setMinimumDuration(1000);
connect(progressDialog , &QProgressDialog::destroyed, ={qDebug() << "progressDialog destroyed";});progressDialog ->setLabelText("Processing ...");
progressDialog ->setCancelButtonText("Abort Processing");connect(progressDialog , &QProgressDialog::canceled, ={
progressDialog ->setLabelText("Aborting...");
progressDialog ->setCancelButton(0);
});progressDialog ->setMinimum(0);
progressDialog ->setMaximum(totalCount); // Each var in varList has some count, total of var counts.
progressDialog ->setValue(0);foreach (Var var, varList) {
Worker *worker = new Worker (var);
QThread *thread = new QThread();
worker ->moveToThread(thread);connect(thread, &QThread::started, worker , &Worker ::performOperation); connect(progressDialog , &QProgressDialog::canceled, worker , &Worker ::onCancel, Qt::DirectConnection); connect(worker , &Worker ::processingFinished, this, &MainWindow::onProcessingFinished); connect(worker , &Worker ::processingFinished, thread, &QThread::quit); connect(thread, &QThread::finished, thread, &QThread::deleteLater); thread->start(); }
progressDialog ->show();
}void onProcessingFinished()
{
Workerworker = qobject_cast<Worker>(sender());
if(worker ) {this->appendResultInTable(worker ->result()); // Function has MutexLocker.
int currentValue = progressDialog ->value();
if(currentValue < 0) currentValue = 0;progressDialog ->setValue(currentValue + worker->var.count);
}
}My MainWindow consist of a TableView with QStandardItemModel the updates the result (processed by Worker)
// Stack Trace
-
@Maaz-Momin said in QProgressDialog or MainWindow with table Crashes when using with MultiThread:
QProgressDialog* progressDialog // Global variable defined in header file.
Why?! Do not use global variables.
If your app is crashing then it is most probably a pointer issue. You should run it through debugger and see what happens if it crashes (you can post the stack trace here, so we can help). -
@jsulm If not global how do you see this? Should i emit another signal from onProcessingFinished to update Progress dialog?
Also i am not deleting progressdialog anywhere. Will update stack trace soon.
-
@Maaz-Momin said in QProgressDialog or MainWindow with table Crashes when using with MultiThread:
If not global how do you see this?
Can be a simple member variable in the class (MainWindow or what ever creates the progress dialog).
-
Yes it is a member variable of a class. Sorry my mistake. Member variable of MainWindow
-
@Maaz-Momin
As a first step, I would suggest new'ingQProgressDialog* progressDialog
inside the MainWindow constructor, with this(the MainWindow) as parent.That guarantees a valid point for the whole existence of the mainwindow, and takes care of your the deletion of progressDialog. At the moment you're leaking memory each time startProcessing is called.
See if that actually fixes it (invalid pointer to progressDialog would be the error this eliminates)
-
@Maaz-Momin Please post whole stack trace (what you posted is cut at the bottom).
-
If I look at your program this is what I understood.
- There is only one progress Dialog created in mainWindow
- You are creating the multiple Worker object & each running in different threads.
- You have connected each of those worker object signals to progress dialog & vice/versa
Now
Can you show me how are you connecting progress value from worker to progress dialog ? Are you making any direct connection to update the progress ?
Are you calling setValue of progressdialog from any of the worker threads ? -
Q) Can you show me how are you connecting progress value from worker to progress dialog ? Are you making any direct connection to update the progress ? Are you calling setValue of progressdialog from any of the worker threads ?
Ans) No i am not connecting Worker to Progress. I wait for Worker to finish and trigger MainWindow's onProcessingFinished() slot. From this slot I call progressDialog ->setValue.
-
@jsulm @J-Hilk @dheerendra I just saw that i am also getting this error message in Issue panel.
"f:\dd\vctools\crt\vcstartup\src\misc\i386\chkstk.asm:99: error: Debugger encountered an exception: Exception at 0x625ab439, code: 0xc00000fd: stack_overflow, flags=0x0 (first chance)"
-
Debugger & trace is the only choice to find the issue. Since code is with you, only you can troubleshoot better. From our side it is only guess work. However some inputs from my side.
- I'm seeing the stack_overflow. Is there any method getting called recursively ?
- You are doing the foreach. How many worker threads you are creating ?
- You are doing the moveToThread of worker. Does worker has any children objects ?
- Just to rule out that issue is happening from ProgressBar, we can remove everything insid onProcessingFinished(). Just have debug statement. Just see how it behaves.
- We can restrict the number of threads to just one, see how it behaves.
Please note that above are some hints for moving forward & not sure shot for troubleshooting.
Tools like WinDBG should clearly point out the issue.
-
@Maaz-Momin said in QProgressDialog or MainWindow with table Crashes when using with MultiThread:
@jsulm @J-Hilk @dheerendra I just saw that i am also getting this error message in Issue panel.
"f:\dd\vctools\crt\vcstartup\src\misc\i386\chkstk.asm:99: error: Debugger encountered an exception: Exception at 0x625ab439, code: 0xc00000fd: stack_overflow, flags=0x0 (first chance)"
Which is what I'd've guessed from this:
@jsulm The stacktrace seems to be almost 1000 table lines. Won't be able to post that many screenshots. Are you looking for something in particular?
Look over your code (and trace). There's probably a signal that's emitted secondary to a slot which is triggered by the same signal. I don't believe this to be a threading issue per se.
-
I traced over my code and but find any signal-slot recursive but i guess I understood the reason as the error suggest it's stack_overflow.
I am trying to read all the files in a folder(recursive) and process the files. My logic was to create each thread with total file size of 50KB.
but when I try this on a large folder say around (3.5MB and above) then only the program crashes.
My Log: '44' Batch(Thread) created for '390' files with total file size 3501128 bytes.
@SGaist @aha_1980 @mrjj @jsulm @J-Hilk @dheerendra @kshegunov Can you guys advise on how to handle Multiple-File reading and processing in MultiThread.
-
I found a way how I can work with this. The anwser is QThread::idealThreadCount. Thank you for your help.