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
Forum Updated to NodeBB v4.3 + New Features

desktop app crashed when 2 event loops are used

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 2.1k 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.
  • R Offline
    R Offline
    rahush
    wrote on 5 Apr 2017, 15:07 last edited by rahush 4 Jun 2017, 05:46
    #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 5 Apr 2017, 15:39
    0
    • R rahush
      5 Apr 2017, 15:07

      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 5 Apr 2017, 15:39 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 5 Apr 2017, 22:18 last edited by SGaist 4 May 2017, 22:19
        #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 6 Apr 2017, 05:40
        0
        • S SGaist
          5 Apr 2017, 22:18

          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 6 Apr 2017, 05:40 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 6 Apr 2017, 05:44
          0
          • R rahush
            6 Apr 2017, 05:40

            @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 6 Apr 2017, 05:44 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
            • B Offline
              B Offline
              BjornW
              wrote on 6 Apr 2017, 07:38 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 6 Apr 2017, 09:47 last edited by Rondog 4 Jun 2017, 09:49
                #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
                • B Offline
                  B Offline
                  BjornW
                  wrote on 6 Apr 2017, 11:39 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

                  1/8

                  5 Apr 2017, 15:07

                  • Login

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