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. [SOLVED] Synchronous Network Request, QEventLoop and dll
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Synchronous Network Request, QEventLoop and dll

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.7k Views 1 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.
  • UndeadBlowU Offline
    UndeadBlowU Offline
    UndeadBlow
    wrote on last edited by
    #1

    Hello guys.
    I have the dll, that just executes one simple network request. I need it to be Synchronous. I made it with QEventLoop. And all was ok... but we tasted it on another PC and our program crashed. It was hard to debug what is porblem in program with dll, but today we finally connected to executing progam with loaded that dll and we have seen:
    @QEventLoop: Cannot be used without QApplication
    QEventLoop: Cannot be used without QApplication
    QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::closeHandle()@
    I was shocked. This program works very well when I executing it on my PC or when we executing it in Qt Creator in debug mode on another PC. Else - it crashed AFTER dll executed, on QLibrary::unload method. I can't understand what is going on and my english is bad, but pls helps me to understand. Thanks!
    Here goes dll cpp file code:
    @
    RESULT Run(const std::string k, void *p) {
    (void)p;
    if (k.empty()) {
    return RESULT_WRONGKEY;
    }
    QString key = QString::fromStdString(k);
    QString reply = PostRequest(key);
    if (reply.isEmpty()) {
    return RESULT_UNKNOWN_ERROR;
    }
    else if (reply == "GoodTry!") {
    return RESULT_WRONGKEY;
    }
    else {
    QString token_url = URL_AUTH;
    token_url = token_url.arg(reply);
    bool success = QDesktopServices::openUrl(token_url);
    if (success) return RESULT_SUCCESS;
    else return RESULT_FAILURE;
    }
    return RESULT_UNKNOWN_ERROR;
    }

    const char *GetProgramName()
    {
    return "Portal Biocad";
    }

    bool IsAvailable(const std::string &key)
    {
    if (key.empty()) {
    return 0;
    }
    QString reply = PostRequest("");
    if (reply.isEmpty()) {
    return 0;
    }
    else {
    return 1;
    }
    }

    QString PostRequest(const QString &key)
    {
    QNetworkAccessManager manager;
    // setting POST request params
    QUrl params;
    params.addQueryItem("key", key);
    // creating request
    QNetworkRequest request;
    request.setUrl(URL_TOKEN);
    request.setHeader(QNetworkRequest::ContentTypeHeader,
    QVariant("application/x-www-form-urlencoded"));

    // executing request
    QEventLoop wait_reply;
    QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),
                     &wait_reply, SLOT(quit()));
    QNetworkReply *answer = manager.post(request, params.encodedQuery());
    wait_reply.exec();
    return QString(answer->readAll());
    

    }@
    P.S.
    Also debugger shows such first chance exception:
    @code: 0x6a6: Invalid RPC server, flags=0x1@

    1 Reply Last reply
    0
    • UndeadBlowU Offline
      UndeadBlowU Offline
      UndeadBlow
      wrote on last edited by
      #2

      Ok now it's clear that porblem in that exception. In debug mode system can handle it, but in release seems that lib thread (i run dll in another thread) crashes and program can't unload dll back. Without unload all ok, but it's not ok. How I can solve it? Maybe turn off exceptions? I tried but seems didn't help or I did something wrong (I wrote CONFIG -= exceptions). Some ideas guys? It's bug or what?

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

        Hi,

        The first error message is want is strange, are you instantiating a QApplication in the software using your dll ?

        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
        • UndeadBlowU Offline
          UndeadBlowU Offline
          UndeadBlow
          wrote on last edited by
          #4

          [quote author="SGaist" date="1415222429"]Hi,

          The first error message is want is strange, are you instantiating a QApplication in the software using your dll ?[/quote]

          Hi. Yes, ofcourse. I was dissapointed too :)
          If run by just run (green arrow) - comes message about QEventLoop that first. If runs with debugger arrow - comes message about that exception and it's first chance.
          If I build all (programm and dll) in debug mode - comes message about hard second chance exception (same code and msg but red).

          Oh, forgot to write: my compiler is MSVC 2012 (2011) and Qt is 4.8.6 (tested 64 and 32 bit, no difference).

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

            By of course, you mean that you have a QApplication created in your software ?

            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
            • UndeadBlowU Offline
              UndeadBlowU Offline
              UndeadBlow
              wrote on last edited by
              #6

              Ok, I found problem and it's have no connection with QEventLoop and requests. I dont know why, but
              @QString key = QString::fromStdString(k);@
              somethimes crashes on normal strings. I change it to
              @QString key = QString key (k.c_str());@
              and all become ok.
              First chance exception still here, but don't affect on apps work.

              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