[SOLVED] Synchronous Network Request, QEventLoop and dll



  • 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@



  • 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?


  • Lifetime Qt Champion

    Hi,

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



  • [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).


  • Lifetime Qt Champion

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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.