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. desktop app crashed when 2 event loops are used

desktop app crashed when 2 event loops are used

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 2.1k 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.
  • R Offline
    R Offline
    rahush
    wrote on last edited by rahush
    #1

    I am trying to write some data to an url using get request from QNetworkAccessManager class. I am creating an event loop in it. There is another event loop running in the main thread which is the main event loop for the application. The application is getting crashed when it reaches eventLoop.exec(); line in the code below. Any idea why is it happening??

    {
    SharedPtr<QNetworkAccessManager> networkAccessManager;
    
    networkAccessManager(new QNetworkAccessManager());
    
    bool res = write(request);
    }
    
    bool Write(QNetworkRequest request) const
    {
        QEventLoop eventLoop;
        connect(networkAccessManager.get(), &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
        int count = 0;
        while (count < retryCount)
        {
            QNetworkReply* reply = networkAccessManager->get(request);
            eventLoop.exec();
            if (reply != NULL && reply->error() == 0)
                return true;
            count++;
        }
        return false;
    }
    

    **networkAccessManager is the QNetworkAccessManager object.

    K 1 Reply Last reply
    0
    • R rahush

      I am trying to write some data to an url using get request from QNetworkAccessManager class. I am creating an event loop in it. There is another event loop running in the main thread which is the main event loop for the application. The application is getting crashed when it reaches eventLoop.exec(); line in the code below. Any idea why is it happening??

      {
      SharedPtr<QNetworkAccessManager> networkAccessManager;
      
      networkAccessManager(new QNetworkAccessManager());
      
      bool res = write(request);
      }
      
      bool Write(QNetworkRequest request) const
      {
          QEventLoop eventLoop;
          connect(networkAccessManager.get(), &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
          int count = 0;
          while (count < retryCount)
          {
              QNetworkReply* reply = networkAccessManager->get(request);
              eventLoop.exec();
              if (reply != NULL && reply->error() == 0)
                  return true;
              count++;
          }
          return false;
      }
      

      **networkAccessManager is the QNetworkAccessManager object.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @rahush

      Hi and welcome to devnet forum

      I have added code markers around your code.
      Please enclose your code snippets with code markers using right button "</>"above your edit window. This makes the code snippets easier to read.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi and welcome to devnet,

        In your code, once your call .get and then ->get(). How can that compile ?

        You should also post the stack trace of your crash.

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

        R 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          In your code, once your call .get and then ->get(). How can that compile ?

          You should also post the stack trace of your crash.

          R Offline
          R Offline
          rahush
          wrote on last edited by
          #4

          @SGaist

          SharedPtr<QNetworkAccessManager> networkAccessManager;

          networkAccessManager(new QNetworkAccessManager());

          networkAccessManager is not an object of QNetworkAccessManager, it is a SharedPtr.

          A 1 Reply Last reply
          0
          • R rahush

            @SGaist

            SharedPtr<QNetworkAccessManager> networkAccessManager;

            networkAccessManager(new QNetworkAccessManager());

            networkAccessManager is not an object of QNetworkAccessManager, it is a SharedPtr.

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @rahush I don't see any obvious reason for the crash.. Can you post the stack trace?

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            0
            • BjornWB Offline
              BjornWB Offline
              BjornW
              wrote on last edited by
              #6

              I've never used an event loop like this. How does the network manager know that it is supposed to post/receive events from the "new" event loop? I see no connection between the two objects.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rondog
                wrote on last edited by Rondog
                #7

                The crash is probably from the connect line:

                connect(networkAccessManager.get(),...
                // should be ?
                connect(networkAccessManager,...)
                

                I don't think any of this would be necessary if you used the signals and slots to handle the events (as opposed to a loop waiting for a response). Just a thought.

                1 Reply Last reply
                1
                • BjornWB Offline
                  BjornWB Offline
                  BjornW
                  wrote on last edited by
                  #8

                  What scope is this?

                  {
                  SharedPtr<QNetworkAccessManager> networkAccessManager;
                  
                  networkAccessManager(new QNetworkAccessManager());
                  
                  bool res = write(request);
                  }
                  
                  

                  You have declared networkAccessManager locally in that scope. What networkAccessManager are you referring to inside the Write method?

                  1 Reply Last reply
                  3

                  • Login

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