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. Application crash on QEventloop::exec() on MacOs
Forum Updated to NodeBB v4.3 + New Features

Application crash on QEventloop::exec() on MacOs

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 1.8k 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.
  • A Offline
    A Offline
    Anj_San
    wrote on last edited by
    #5

    @kshegunov This is my implementation:-

    QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url)
    {

    QUrl serviceUrl = QUrl(url);
    QNetworkRequest request(serviceUrl);
    request.setRawHeader("Authorization",token.toUtf8());
    
    Networkmanager->get(request);
    
    QObject::connect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*)));
    loop.exec();
    
    disconnect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*)));
    
        return user_contact;
    

    }

    void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply)
    {

         QVariant Status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    
        QByteArray buffer = reply->readAll();
        QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer));
        qDebug() << jsonDoc;
        QJsonObject jsonReply = jsonDoc.object();
        if(Status_code.toInt() == 200)
        {
            if(jsonReply.contains("userlist"))
            {
                user_contact = jsonReply["userlist"].toArray();
            }
    
            if(jsonReply.contains("message")){
                message = jsonReply["message"].toString();
            }
    
        }
        else if(Status_code.toInt() == 422)
        {
            message = jsonReply["message"].toString();
            qDebug()<<message;
    
        }
       else
        {
            message = "Could not connect at the moment";
        }
    
    reply->deleteLater();
    loop.exit(0);
    

    }

    kshegunovK 1 Reply Last reply
    0
    • A Anj_San

      @kshegunov This is my implementation:-

      QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url)
      {

      QUrl serviceUrl = QUrl(url);
      QNetworkRequest request(serviceUrl);
      request.setRawHeader("Authorization",token.toUtf8());
      
      Networkmanager->get(request);
      
      QObject::connect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*)));
      loop.exec();
      
      disconnect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*)));
      
          return user_contact;
      

      }

      void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply)
      {

           QVariant Status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
      
          QByteArray buffer = reply->readAll();
          QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer));
          qDebug() << jsonDoc;
          QJsonObject jsonReply = jsonDoc.object();
          if(Status_code.toInt() == 200)
          {
              if(jsonReply.contains("userlist"))
              {
                  user_contact = jsonReply["userlist"].toArray();
              }
      
              if(jsonReply.contains("message")){
                  message = jsonReply["message"].toString();
              }
      
          }
          else if(Status_code.toInt() == 422)
          {
              message = jsonReply["message"].toString();
              qDebug()<<message;
      
          }
         else
          {
              message = "Could not connect at the moment";
          }
      
      reply->deleteLater();
      loop.exit(0);
      

      }

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

      Don't use local event loops, especially where when there's no pressing need to. You're spinning the global even loop and you have no idea what events are processed in the meantime. Here's a suggestion:

      QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url)
      {
          QNetworkRequest request(QUrl(url));
          request.setRawHeader("Authorization", token.toUtf8());
      
          QNetworkReply * reply = Networkmanager->get(request);
      
          QObject::connect(reply, &QNetworkReply::finished, this, std::bind(&Contact_API_Comm::serviceContactRequestFinished, this, reply));
          QObject::connect(reply, &QNetworkReply::finished, reply, &QObject::deleteLater);
      }
      
      void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply)
      {
          QJsonArray result;
          // Read the json data into `result`
      
          emit contactReceived(result); //< This signal you declare yourself and connect something to it to process the data further
      }

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4
      • A Offline
        A Offline
        Anj_San
        wrote on last edited by
        #7

        @kshegunov I have tried this but now the app is crashing with this stack trace

        1 objc_msgSend (x86_64h) /usr/lib/libobjc.A.dylib 0x7fff6051b6a9
        2 (anonymous namespace)::AutoreleasePoolPage::pop(void *) (x86_64h) /usr/lib/libobjc.A.dylib 0x7fff6051e47c
        3 _CFAutoreleasePoolPop (x86_64h) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation 0x7fff35d4fd4a
        4 -[NSAutoreleasePool drain] (x86_64) /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation 0x7fff37ffa792
        5 -[NSApplication run] (x86_64) /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit 0x7fff3336c5f7
        6 QCocoaEventDispatcher::processEvents(QFlagsQEventLoop::ProcessEventsFlag) (x86_64) /Users/x/Qt/5.12.6/clang_64/plugins/platforms/libqcocoa.dylib 0x1029fd013
        7 QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) (x86_64) /Users/x/Qt/5.12.6/clang_64/lib/QtCore.framework/Versions/5/QtCore 0x1018b1b8f
        8 QCoreApplication::exec() (x86_64) /Users/x/Qt/5.12.6/clang_64/lib/QtCore.framework/Versions/5/QtCore 0x1018b6b82
        9 main (x86_64) /Users/x/build-Aact-Desktop_Qt_5_12_6_clang_64bit-Release/Aact.app/Contents/MacOS/Aact 0x100079e7b
        10 start (x86_64) /usr/lib/system/libdyld.dylib 0x7fff61cf73d5

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

          Hi,

          What version of macOS are you running ?

          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
          0
          • A Offline
            A Offline
            Anj_San
            wrote on last edited by
            #9

            Hi @SGaist
            My MacOS version is 10.14.6

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

              You seem to have a lot of QEventLoop instances spread over your code, why so many ?

              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
              0
              • A Offline
                A Offline
                Anj_San
                wrote on last edited by
                #11

                I used it for API call and response. Is using too many even loop instance is a wrong?

                jsulmJ 1 Reply Last reply
                0
                • A Anj_San

                  I used it for API call and response. Is using too many even loop instance is a wrong?

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

                  @Anj_San said in Application crash on QEventloop::exec() on MacOs:

                  Is using too many even loop instance is a wrong?

                  Yes. Use assynchronous nature of Qt like @kshegunov shown

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

                  1 Reply Last reply
                  2
                  • A Offline
                    A Offline
                    Anj_San
                    wrote on last edited by
                    #13

                    @jsulm I have tried @kshegunov 's way but the code became more complicated and me confused.
                    Is there no way to save it while using qEventLoop::exec()?

                    jsulmJ 1 Reply Last reply
                    0
                    • A Anj_San

                      @jsulm I have tried @kshegunov 's way but the code became more complicated and me confused.
                      Is there no way to save it while using qEventLoop::exec()?

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

                      @Anj_San You should really learn how to use assynchronous APIs properly instead of making it synchronous with event loops all over the place.

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

                      1 Reply Last reply
                      1

                      • Login

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