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. Debugging a Thread in Qt Creator
Forum Updated to NodeBB v4.3 + New Features

Debugging a Thread in Qt Creator

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 14.6k Views 3 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.
  • K Offline
    K Offline
    kumararajas
    wrote on last edited by kumararajas
    #1

    Hi,

    Here is the scenario -

    Main process creates Qt Application in a thread and all the Qt resources are being managed over there. And the main process continues perform other activities.

    Here, I would like to debug the Qt app which runs on a thread.

    I did try many ways but still unable to succeed.

    I did enable "Debug All Children", I did add a command "set follow-fork-mode child".

    Still no success.

    Whereas, I could debug my application from Eclipse IDE. Question is why not Qt Creator?!

    Can anyone please help me out?

    Thank you,
    Kumara

    --Kumar

    jsulmJ 1 Reply Last reply
    0
    • K kumararajas

      Hi,

      Here is the scenario -

      Main process creates Qt Application in a thread and all the Qt resources are being managed over there. And the main process continues perform other activities.

      Here, I would like to debug the Qt app which runs on a thread.

      I did try many ways but still unable to succeed.

      I did enable "Debug All Children", I did add a command "set follow-fork-mode child".

      Still no success.

      Whereas, I could debug my application from Eclipse IDE. Question is why not Qt Creator?!

      Can anyone please help me out?

      Thank you,
      Kumara

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

      @kumararajas "unable to succeed" - can you explain a bit more? What does not work? It doesn't stop at a break point? Something else?
      Also, it is unusual to put UI in another thread. Why not keep UI in the main thread and do other stuff in another thread?

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

      K 1 Reply Last reply
      0
      • jsulmJ jsulm

        @kumararajas "unable to succeed" - can you explain a bit more? What does not work? It doesn't stop at a break point? Something else?
        Also, it is unusual to put UI in another thread. Why not keep UI in the main thread and do other stuff in another thread?

        K Offline
        K Offline
        kumararajas
        wrote on last edited by
        #3

        @jsulm

        Yes, it does not hit the breakpoint in the thread.

        And UI being on thread, In my project UI is not a main application. UI is optional. So, there is a main application which creates the thread for UI, if UI is configured.

        --Kumar

        J.HilkJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Qt UI must run on main thread. see http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          K 1 Reply Last reply
          1
          • K kumararajas

            @jsulm

            Yes, it does not hit the breakpoint in the thread.

            And UI being on thread, In my project UI is not a main application. UI is optional. So, there is a main application which creates the thread for UI, if UI is configured.

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

            @kumararajas

            I'm sorry, but I'll have to ask ;)
            You're sure, that you create a debug, not realease, build and that you start the Qt-Debugger, F5-key not CTRL+R?


            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.

            K 1 Reply Last reply
            0
            • VRoninV VRonin

              Qt UI must run on main thread. see http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

              K Offline
              K Offline
              kumararajas
              wrote on last edited by
              #6

              @VRonin Can we say GUIThread and the MainThread are same?
              I see as GUIThread is the one who creates QApplication and the other threads cannot have an access to it. In my case, I do not have a worker thread which does job for Qt, instead my Qt Thread does create Qt resources and accesses them.

              Do you see a problem with it?

              --Kumar

              VRoninV 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @kumararajas

                I'm sorry, but I'll have to ask ;)
                You're sure, that you create a debug, not realease, build and that you start the Qt-Debugger, F5-key not CTRL+R?

                K Offline
                K Offline
                kumararajas
                wrote on last edited by
                #7

                @J.Hilk Thanks for asking :) I build the application in debug mode and I press F5 to start the debug process.

                --Kumar

                kshegunovK 1 Reply Last reply
                0
                • K kumararajas

                  @VRonin Can we say GUIThread and the MainThread are same?
                  I see as GUIThread is the one who creates QApplication and the other threads cannot have an access to it. In my case, I do not have a worker thread which does job for Qt, instead my Qt Thread does create Qt resources and accesses them.

                  Do you see a problem with it?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  form http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

                  As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread.

                  So the Qt GUI must be spawned by the same thread the OS originates, it cannot be on any other thread regardless of where QApplication is living

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  kshegunovK 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    form http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

                    As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread.

                    So the Qt GUI must be spawned by the same thread the OS originates, it cannot be on any other thread regardless of where QApplication is living

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

                    @VRonin said in Debugging a Thread in Qt Creator:

                    So the Qt GUI must be spawned by the same thread the OS originates, it cannot be on any other thread regardless of where QApplication is living

                    Not true. Any thread can be used as the GUI thread, and although unusual it may not be the main thread, i.e. main().

                    Read and abide by the Qt Code of Conduct

                    VRoninV 1 Reply Last reply
                    0
                    • K kumararajas

                      @J.Hilk Thanks for asking :) I build the application in debug mode and I press F5 to start the debug process.

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

                      Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted encountered a bug on the tracker that corresponds to the behaviour you're describing.

                      Edit: Original thread with the corresponding bug report https://forum.qt.io/topic/74646/qt-creator-fails-to-map-breakpoints-sometimes

                      Read and abide by the Qt Code of Conduct

                      K 2 Replies Last reply
                      0
                      • kshegunovK kshegunov

                        @VRonin said in Debugging a Thread in Qt Creator:

                        So the Qt GUI must be spawned by the same thread the OS originates, it cannot be on any other thread regardless of where QApplication is living

                        Not true. Any thread can be used as the GUI thread, and although unusual it may not be the main thread, i.e. main().

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @kshegunov Probably a linguistic mistake on my part but that's how I read that paragraph. I think some platforms (Windows?) force that restriction

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        kshegunovK 1 Reply Last reply
                        0
                        • VRoninV VRonin

                          @kshegunov Probably a linguistic mistake on my part but that's how I read that paragraph. I think some platforms (Windows?) force that restriction

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

                          @VRonin said in Debugging a Thread in Qt Creator:

                          I think some platforms (Windows?) force that restriction

                          I don't believe so, although don't hold me to it.

                          Also see this post:
                          https://forum.qt.io/topic/67285/ui-application-as-thread-cpu-load-almost-100/12

                          I can vouch it works fine on Linux for sure.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            From Jasmin Blanchette, Mark Summerfield, C++ GUI Programming with Qt 4 Chapter 18, Pragraph "Communicating with the Main Thread"

                            When a Qt application starts, only one thread is runningthe main thread. This is the only thread that is allowed to create the QApplication or QCoreApplication object and call exec() on it. After the call to exec(), this thread is either waiting for an event or processing an event.

                            This is what put that concept into my mind. I very well admit it might not be the case, wouldn't be the first time that manual was wrong

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            1
                            • K Offline
                              K Offline
                              kumararajas
                              wrote on last edited by
                              #14

                              Thanks for the discussions on GUIThread vs main thread.

                              However, does it stops me debugging?

                              Thanks!
                              Kumara

                              --Kumar

                              JKSHJ 1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted encountered a bug on the tracker that corresponds to the behaviour you're describing.

                                Edit: Original thread with the corresponding bug report https://forum.qt.io/topic/74646/qt-creator-fails-to-map-breakpoints-sometimes

                                K Offline
                                K Offline
                                kumararajas
                                wrote on last edited by
                                #15

                                @kshegunov said in Debugging a Thread in Qt Creator:

                                Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted a bug on the tracker that corresponds to the behaviour you're describing.

                                I am gonna try this now and get back to you with the results.

                                Thanks for the thoughts!

                                --Kumar

                                1 Reply Last reply
                                0
                                • K kumararajas

                                  Thanks for the discussions on GUIThread vs main thread.

                                  However, does it stops me debugging?

                                  Thanks!
                                  Kumara

                                  JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @VRonin said in Debugging a Thread in Qt Creator:

                                  @kshegunov Probably a linguistic mistake on my part but that's how I read that paragraph. I think some platforms (Windows?) force that restriction

                                  I think that paragraph is poorly-written. On Windows and Linux, you can use a non-main thread as your GUI thread. macOS is the one that has the restriction. See http://stackoverflow.com/questions/22289423/how-to-avoid-qt-app-exec-blocking-main-thread/22290909#22290909

                                  (Side note: LQ Widgets spawns a non-main GUI thread. If you're interested in the details, see startWidgetEngine() and run() in https://github.com/JKSH/LQWidgets/blob/master/src/Cpp/lqmain.cpp )

                                  @kumararajas said in Debugging a Thread in Qt Creator:

                                  Thanks for the discussions on GUIThread vs main thread.

                                  However, does it stops me debugging?

                                  Good question; I don't know the answer. Please let us know your results.

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  kshegunovK 1 Reply Last reply
                                  2
                                  • JKSHJ JKSH

                                    @VRonin said in Debugging a Thread in Qt Creator:

                                    @kshegunov Probably a linguistic mistake on my part but that's how I read that paragraph. I think some platforms (Windows?) force that restriction

                                    I think that paragraph is poorly-written. On Windows and Linux, you can use a non-main thread as your GUI thread. macOS is the one that has the restriction. See http://stackoverflow.com/questions/22289423/how-to-avoid-qt-app-exec-blocking-main-thread/22290909#22290909

                                    (Side note: LQ Widgets spawns a non-main GUI thread. If you're interested in the details, see startWidgetEngine() and run() in https://github.com/JKSH/LQWidgets/blob/master/src/Cpp/lqmain.cpp )

                                    @kumararajas said in Debugging a Thread in Qt Creator:

                                    Thanks for the discussions on GUIThread vs main thread.

                                    However, does it stops me debugging?

                                    Good question; I don't know the answer. Please let us know your results.

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

                                    @JKSH said in Debugging a Thread in Qt Creator:

                                    Side note: LQ Widgets spawns a non-main GUI thread.

                                    You have a race condition on initialization, though ... you should switch Bridge * to QAtomicPointer<Bridge>, since at lines 32 and 57 you touch the pointer from different threads. By the way, what's the problem with using a semaphore to wait for the GUI thread, I really dislike that while (!bridge) line ...

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    3
                                    • kshegunovK kshegunov

                                      Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted encountered a bug on the tracker that corresponds to the behaviour you're describing.

                                      Edit: Original thread with the corresponding bug report https://forum.qt.io/topic/74646/qt-creator-fails-to-map-breakpoints-sometimes

                                      K Offline
                                      K Offline
                                      kumararajas
                                      wrote on last edited by
                                      #18

                                      @kshegunov said in Debugging a Thread in Qt Creator:

                                      Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted encountered a bug on the tracker that corresponds to the behaviour you're describing.

                                      Hi,

                                      I did give a try with this approach. Still no success on not hitting the breakpoint in thread..

                                      --Kumar

                                      kshegunovK 1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        kumararajas
                                        wrote on last edited by
                                        #19

                                        And here is one observation on Qt Application being on main thread vs worker thread.

                                        When you run Qt application on a main thread, you don't create the thread by yourself, so you don't need to worry about the thread parameters which has been used by Qt internally.

                                        When we create Qt application on a thread, we are suppose to take care of thread parameters.

                                        Example, thread stack size. If you don't set it by yourself, then the linux default stack size for the thread is 2MB. Which will be used for your Qt thread.
                                        And the challenge here is, we don't know what should be the stack size for Qt. We may overrun during long run, so we should keep that in mind. And the challenge is Qt never tell us on how much should be set.

                                        I have observed this just today morning. I have set my stack size to 64k and the application was coming up and getting exited in few seconds with segmentation fault error. When I have increased to 128K, application was up for a while, but it crashed later on. Now, I have 256K, which looks good but I never know when it will crash.

                                        Having the stack size challenge aside, I don't think any problem on Qt being on a worker thread.

                                        --Kumar

                                        1 Reply Last reply
                                        0
                                        • K kumararajas

                                          @kshegunov said in Debugging a Thread in Qt Creator:

                                          Try disabling "Run in terminal" in Qt creator if you haven't tried that. Recently I posted encountered a bug on the tracker that corresponds to the behaviour you're describing.

                                          Hi,

                                          I did give a try with this approach. Still no success on not hitting the breakpoint in thread..

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

                                          @kumararajas said in Debugging a Thread in Qt Creator:

                                          I did give a try with this approach. Still no success on not hitting the breakpoint in thread..

                                          Well, what about the debugger log? It should shed some light on the issue.

                                          When we create Qt application on a thread, we are suppose to take care of thread parameters.

                                          Not really, no.

                                          Example, thread stack size. If you don't set it by yourself, then the linux default stack size for the thread is 2MB.

                                          Which is perfectly fine, I wouldn't mess with the stack size for no good reason.

                                          Now, I have 256K, which looks good but I never know when it will crash.

                                          Then just leave it to the OS!

                                          Read and abide by the Qt Code of Conduct

                                          K 1 Reply Last reply
                                          2

                                          • Login

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