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. Cannot create children for a parent that is in a different thread.

Cannot create children for a parent that is in a different thread.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.6k Views
  • 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.
  • B Offline
    B Offline
    BlueManCZ
    wrote on last edited by BlueManCZ
    #1

    I have an object instance of the class (let's say controller) with QNetworkManager instance in one of its fields. The instance of this controller is created in one thread. Now, I access and use this controller from other program threads and everything works fine. But when the controller sends an HTTP request, I get this warning:

    [warn:default] QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x55969f574bc0), parent's thread is QThread(0x55969f22aee0), current thread is QThreadPoolThread(0x7f155005cb70)
    

    I understand this warning, it's well explained in multiple posts on this forum.

    But my code works without any problems and I don't feel the pressure to change anything. Is there any way to get rid of these warnings?


    I would provide the code, but it's extensive, so I link it up against my GitHub repository:

    • Instance of QNetworkManager
    • Simple HTTP request sent through this manager
    • Instance of the controller
    • Usage of the controller from different thread
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There's your issue. I am guessing your are moving an instance of SpotifyWebAPI to a different thread. However, your QNAM instance is not moved as it's not parented to its containing class. Therefore it still lives in the thread that created the SpotifyWebAPI instance. Give it a parent.

      It will have the added benefit of also removing the leak you have here since you do not destroy the QNAM object in your destructor.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        BlueManCZ
        wrote on last edited by BlueManCZ
        #3

        Hello @SGaist,

        thank you for your reply. I guess you could be right, it makes sense. I made those changes but warnings are still there.

        Am I missing something else?

        SpotifyWebAPI instance is created in: Constructor thread QThread(0x55969f574bc0).
        After that, methods of this instance are called from pooled threads: QThreadPoolThread(0x7f155005cb70, name = "Thread (pooled)").

        When I create a new instance of SpotifyWebAPI in each pooled thread, it solves the warning, but I need one persistent instance of SpotifyWebAPI for all pooled threads.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BlueManCZ
          wrote on last edited by BlueManCZ
          #4

          I managed to reduce the frequency of warnings by creating a new instance of QNAM in each thread, but it is still not optimal, because those threads run parallel and sometimes happens that one thread accesses QNAM instance created by another thread.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @BlueManCZ said in Cannot create children for a parent that is in a different thread.:

            sometimes happens that one thread accesses QNAM instance created by another thread.

            So fix it - this must not happen to make it work correctly.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • B Offline
              B Offline
              BlueManCZ
              wrote on last edited by
              #6
              if (manager->thread() != QThread::currentThread()) {
                  return false;
              }
              
              QNetworkReply *reply = manager->get(request);
              

              I added this condition to avoid thread to use QNAM instance created by another thread, and it seems to work fine. The only thing that worries me now is how QNAM instance is created very often and sometimes (in case of this condition) quite unnecessarily.

              Maybe it is not a big deal? Is QNAM object so lightweight that I can afford to create it quite often?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I don't see a need for user threads for network operations at all...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                B 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  I don't see a need for user threads for network operations at all...

                  B Offline
                  B Offline
                  BlueManCZ
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher said in Cannot create children for a parent that is in a different thread.:

                  I don't see a need for user threads for network operations at all...

                  Well, my code is an extension for the bigger project. Query handlers of all extensions in this project are called in parallel threads and I can't do anything with it.

                  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