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. QProgressDialog or MainWindow with table Crashes when using with MultiThread
Forum Updated to NodeBB v4.3 + New Features

QProgressDialog or MainWindow with table Crashes when using with MultiThread

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 1.8k Views 1 Watching
  • 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.
  • Maaz MominM Maaz Momin

    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 Trace0_1547109543377_Plugin_Crash.JPG

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @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).

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • Maaz MominM Offline
      Maaz MominM Offline
      Maaz Momin
      wrote on last edited by Maaz Momin
      #3

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

      jsulmJ 1 Reply Last reply
      0
      • Maaz MominM Maaz Momin

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @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).

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Maaz MominM Offline
          Maaz MominM Offline
          Maaz Momin
          wrote on last edited by
          #5

          Yes it is a member variable of a class. Sorry my mistake. Member variable of MainWindow

          J.HilkJ 1 Reply Last reply
          0
          • Maaz MominM Maaz Momin

            Yes it is a member variable of a class. Sorry my mistake. Member variable of MainWindow

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @Maaz-Momin
            As a first step, I would suggest new'ing QProgressDialog* 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)


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • Maaz MominM Offline
              Maaz MominM Offline
              Maaz Momin
              wrote on last edited by Maaz Momin
              #7

              @jsulm I have updated StackTrace.

              @J-Hilk As you can the parenting is already done in start processing function. Edit: Also in constructor i have initialized it to NULL

              jsulmJ 1 Reply Last reply
              0
              • Maaz MominM Maaz Momin

                @jsulm I have updated StackTrace.

                @J-Hilk As you can the parenting is already done in start processing function. Edit: Also in constructor i have initialized it to NULL

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Maaz-Momin Please post whole stack trace (what you posted is cut at the bottom).

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Maaz MominM 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Maaz-Momin Please post whole stack trace (what you posted is cut at the bottom).

                  Maaz MominM Offline
                  Maaz MominM Offline
                  Maaz Momin
                  wrote on last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by dheerendra
                    #10

                    If I look at your program this is what I understood.

                    1. There is only one progress Dialog created in mainWindow
                    2. You are creating the multiple Worker object & each running in different threads.
                    3. 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 ?

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    Maaz MominM 1 Reply Last reply
                    0
                    • dheerendraD dheerendra

                      If I look at your program this is what I understood.

                      1. There is only one progress Dialog created in mainWindow
                      2. You are creating the multiple Worker object & each running in different threads.
                      3. 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 ?

                      Maaz MominM Offline
                      Maaz MominM Offline
                      Maaz Momin
                      wrote on last edited by
                      #11

                      @dheerendra

                      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.

                      1 Reply Last reply
                      0
                      • Maaz MominM Offline
                        Maaz MominM Offline
                        Maaz Momin
                        wrote on last edited by
                        #12

                        @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)"

                        kshegunovK 1 Reply Last reply
                        0
                        • dheerendraD Offline
                          dheerendraD Offline
                          dheerendra
                          Qt Champions 2022
                          wrote on last edited by
                          #13

                          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.

                          1. I'm seeing the stack_overflow. Is there any method getting called recursively ?
                          2. You are doing the foreach. How many worker threads you are creating ?
                          3. You are doing the moveToThread of worker. Does worker has any children objects ?
                          4. 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.
                          5. 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.

                          Dheerendra
                          @Community Service
                          Certified Qt Specialist
                          http://www.pthinks.com

                          1 Reply Last reply
                          1
                          • Maaz MominM Maaz Momin

                            @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)"

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #14

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

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • Maaz MominM Offline
                              Maaz MominM Offline
                              Maaz Momin
                              wrote on last edited by
                              #15

                              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.

                              1 Reply Last reply
                              0
                              • Maaz MominM Offline
                                Maaz MominM Offline
                                Maaz Momin
                                wrote on last edited by
                                #16

                                I found a way how I can work with this. The anwser is QThread::idealThreadCount. Thank you for your help.

                                1 Reply Last reply
                                0

                                • Login

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