QNetworkRequest / QNetworkReply issue with 5.13.1?



  • I am fairly new to Qt, so I apologize if I am writing this in the wrong place, but I wanted to notify someone of a possible bug, or maybe I am just bad at programming :P

    I recently installed Qt 5.13.1 and all of a sudden I ran into issues pertaining to, or at least what I think pertains to, QNetworkRequest || QNetworkReply or possibly both.

    I was able to track down in my code the piece of code that crashes my program in 5.13.1.

    I have a server that operates a RESTful API and I built a function within my program to make "GET" request and return the JSON document.

    This code runs fine once, however, if ran again, it crashes the program in 5.13.1 (but works fine in 5.13.0 as well as versions previous to this).

    All the member variables are set in the "MainWindow" construct function. If you need me to paste how they are set, let me know.

    Here is the function that will only run ONCE on 5.13.1 and then crashes on the second time it is called:

    QJsonDocument MainWindow::getRequest(QString path)
    {
        qDebug() << "Running path: "+path;
    
        QUrl matrixAPI;
        matrixAPI.setUrl(m_api_url);
        matrixAPI.setPath(path);
        matrixAPI.setPort(m_api_port);
    
        QString concatenated = m_username + ":" + m_password;
        QByteArray userString = concatenated.toLocal8Bit().toBase64();
        QString headerData = "Bearer " + userString;
    
        QNetworkRequest networkRequest;
        networkRequest.setUrl(matrixAPI);
        networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
    
        QNetworkReply *reply = m_restclient->get(networkRequest);
        while (!reply->isFinished())
        {
            QCoreApplication::processEvents();
        }
    
        QJsonDocument json;
    
        if(reply->error() != QNetworkReply::NoError)
        {
            qDebug() << "FAILED:" << reply->error();
            m_restclient->clearAccessCache();
        }
        else
        {
            //parse the reply JSON and display result in the UI
            json = QJsonDocument::fromJson(reply->readAll());
        }
    
        qDebug() << "JSON Data: " << json; 
        reply->deleteLater();
    
        qDebug() << "Return JSON Obj";
        return json;
    
    }
    

    Am I doing something wrong or is this a bug in 5.13.1?

    5.12.3 works on desktop and mobile
    5.13.0 works on desktop and mobile
    5.13.1 works on mobile, CRASHES on desktop during 2nd call


  • Qt Champions 2018

    Where exactly does it crash and why do you spin an eventloop instead properly relying on signals/slots?



  • Why do you spin an eventloop instead properly relying on signals/slots?

    When I first started programming with Qt I relied heavily on examples that I found over the internet. I could not find any good examples of retrieving API JSON data on the Qt website and had to rely on other sources. The code you see is a combination of examples I found that I got to work for the past 2 years. Now, with the advent of 5.13.1, the code does not work and will probably need to be updated to the proper signal / slot procedure which may fix the crash issue.

    Where does it crash?

    The function crashes the SECOND time it runs the function right at:

    reply->deleteLater();
    

    I added a qDebug() right before it and after it. The first time through both qDebug()s echo'd out the info. The second time through only the first qDebug() echo'd out info and then crashed on the reply->deleteLater() line.

    I would like to Add

    One thing I would like to add, not sure if this would play a part. All these calls are being made from child windows (classes). I create a pointer object in a child window (class) in the construct function that points to the MainWindow class, then I call the api GET function that resides within the MainWindow class like:

    //mw is a pointer to the MainWindow class
    QJsonObject api_info = mw->getRequest("some/api/link").object();
    

  • Qt Champions 2018

    @AwesomeBoxhead said in QNetworkRequest / QNetworkReply issue with 5.13.1?:

    reply->readAll()

    So readAll() works but deleteLater() crashes? Please show us the backtrace.



  • That is correct.

    The first time the function "MainWindow::getRequest" is ran:

    • qDebug() before deleteLater() echo'd the JSON object returned from json = QJsonDocument::fromJson(reply->readAll());
    • deleteLater() runs
    • qDebug() runs after deleteLater()

    The second time the function runs:

    • qDebug() before deleteLater() echo'd the JSON object returned from json = QJsonDocument::fromJson(reply->readAll());
    • deleteLater() runs and CRASH

    In regards to the backtrace, I am having one heck of a time trying to find documentation on how to do this. I do not want to deviate this topic with how to generate a backtrace, so I will open a new topic and will get back with you once I become more educated, lol

    Thanks for your time looking at this!

    ~Dustin


  • Qt Champions 2018

    Can you please add the qDebug() statements in your code?
    And for getting the backtrace see here: https://doc.qt.io/qtcreator/creator-debug-mode.html



  • I edited the original post and added the qDebug() before AND after the reply->deleteLater() like I have in my code.

    I activated debug mode which is really cool.

    These are the steps I followed within debug mode:

    <app launched>
    Clicked the menu
    Clicked Login link
    <the login window opens>
    Clicked Login button
    <error window opens, failed logging in>
    Clicked Login button again
    <crash>

    Within debug mode, I received the following error message upon clicking the button for a second time.

    signal-received.png

    Here is the full stacktrace. Gonna be honest, I have no idea what I am looking at with this stacktrace thing, lol. I'm gonna be looking into how to work with this. This debug mode is very neat. Wish I knew about this feature awhile ago, lol.

    1 vtable for QEventDispatcherWin32Private 0x6bcdf840
    2 DirectShowIOReader::readyRead directshowioreader.cpp 347 0x31a5689a
    3 DirectShowIOReader::customEvent directshowioreader.cpp 337 0x31a56c6f
    4 QObject::event qobject.cpp 1282 0x6b94b9d0
    5 QApplicationPrivate::notify_helper qapplication.cpp 3703 0x1ce5832e
    6 QApplication::notify qapplication.cpp 3059 0x1ce5f3d1
    7 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1095 0x6b91eafa
    8 QCoreApplication::sendEvent qcoreapplication.cpp 1490 0x6b91ed22
    9 DirectShowEventLoop::processEvents directshoweventloop.cpp 147 0x31a525e9
    10 DirectShowEventLoop::customEvent directshoweventloop.cpp 127 0x31a5270b
    11 QObject::event qobject.cpp 1282 0x6b94b9d0
    12 QApplicationPrivate::notify_helper qapplication.cpp 3703 0x1ce5832e
    13 QApplication::notify qapplication.cpp 3059 0x1ce5f3d1
    14 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1095 0x6b91eafa
    15 QCoreApplication::sendEvent qcoreapplication.cpp 1490 0x6b91ed22
    16 QCoreApplicationPrivate::sendPostedEvents qcoreapplication.cpp 1840 0x6b9230db
    17 QEventDispatcherWin32::sendPostedEvents qeventdispatcher_win.cpp 1098 0x6b9719a1
    18 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 81 0x2afa92d0
    19 qt_internal_proc qeventdispatcher_win.cpp 245 0x6b9749ab
    20 USER32!CallWindowProcW 0x7ff86d6263ed
    21 USER32!DispatchMessageW 0x7ff86d625de2
    22 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 639 0x6b97415c
    23 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x2afa92b7
    24 QEventLoop::processEvents qeventloop.cpp 138 0x6b91d107
    25 QEventLoop::exec qeventloop.cpp 225 0x6b91d525
    26 QDialog::exec qdialog.cpp 602 0x1d03e75e
    27 loginWindow::escErrorMsg loginwindow.cpp 43 0x4106c0
    28 loginWindow::on_btn_login_clicked loginwindow.cpp 71 0x410a2d
    29 loginWindow::qt_static_metacall moc_loginwindow.cpp 92 0x41ff28
    30 loginWindow::qt_metacall moc_loginwindow.cpp 139 0x420173
    31 QMetaObject::metacall qmetaobject.cpp 309 0x6b9283b4
    32 QMetaObject::activate qobject.cpp 3825 0x6b94a1a3
    33 QMetaObject::activate qobject.cpp 3660 0x6b94a42c
    34 QAbstractButton::clicked moc_qabstractbutton.cpp 313 0x1cf413e7
    35 QAbstractButtonPrivate::emitClicked qabstractbutton.cpp 414 0x1cf41602
    36 QAbstractButtonPrivate::click qabstractbutton.cpp 407 0x1cf42ba0
    37 QAbstractButton::mouseReleaseEvent qabstractbutton.cpp 1011 0x1cf42e41
    38 QWidget::event qwidget.cpp 8959 0x1ce99958
    39 QAbstractButton::event qabstractbutton.cpp 968 0x1cf43ffc
    40 QPushButton::event qpushbutton.cpp 684 0x1cfd284c
    41 QApplicationPrivate::notify_helper qapplication.cpp 3703 0x1ce5832e
    42 QApplication::notify qapplication.cpp 3163 0x1ce5f6b0
    43 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1095 0x6b91eafa
    44 QCoreApplication::sendSpontaneousEvent qcoreapplication.cpp 1502 0x6b91ed40
    45 QApplicationPrivate::sendMouseEvent qapplication.cpp 2649 0x1ce5ea54
    46 QWidgetWindow::handleMouseEvent qwidgetwindow.cpp 662 0x1ceae7ba
    47 QWidgetWindow::event qwidgetwindow.cpp 281 0x1ceb0c43
    48 QApplicationPrivate::notify_helper qapplication.cpp 3703 0x1ce5832e
    49 QApplication::notify qapplication.cpp 3059 0x1ce5f3d1
    50 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1095 0x6b91eafa
    51 QCoreApplication::sendSpontaneousEvent qcoreapplication.cpp 1502 0x6b91ed40
    52 QGuiApplicationPrivate::processMouseEvent qguiapplication.cpp 2111 0x1979463
    53 QGuiApplicationPrivate::processWindowSystemEvent qguiapplication.cpp 1846 0x197a7fc
    54 QWindowSystemInterface::sendWindowSystemEvents qwindowsysteminterface.cpp 1148 0x19609f3
    55 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 82 0x2afa92d9
    56 qt_internal_proc qeventdispatcher_win.cpp 245 0x6b9749ab
    57 USER32!CallWindowProcW 0x7ff86d6263ed
    58 USER32!DispatchMessageW 0x7ff86d625de2
    59 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 639 0x6b97415c
    60 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x2afa92b7
    61 QEventLoop::processEvents qeventloop.cpp 138 0x6b91d107
    62 QEventLoop::exec qeventloop.cpp 225 0x6b91d525
    63 QCoreApplication::exec qcoreapplication.cpp 1403 0x6b9262e8
    64 QGuiApplication::exec qguiapplication.cpp 1788 0x197080f
    65 QApplication::exec qapplication.cpp 2859 0x1ce58210
    66 qMain main.cpp 10 0x4015c3
    67 WinMain qtmain_win.cpp 97 0x4233ba
    68 __tmainCRTStartup 0x4013c7
    69 WinMainCRTStartup 0x4014cb



  • Hi, if you don't try to login a 2nd time from inside the same dialog box, but instead after the 1st failed login close/cancel the dialog box.
    Then select the login link from the menu again, and try login.
    Does it still crash at deleteLater()?


  • Qt Champions 2018

    Since I don't see any call of MainWindow::getRequest() I doubt it happens in this function at all. I would take a look at loginWindow::on_btn_login_clicked() function.



  • @hskoglund

    Thanks for taking the time to read!

    Yes, unfortunately it does still crash even if I try closing the login window after the first failed attempt.

    My app uses many windows that calls the api. Even upon a successful login, the next window that calls "MainWindow::getRequest()" will crash in version 5.13.1.


  • Qt Champions 2018

    So there must be some common problem not related to QNetworkRequest (as we can see in the backtrace). I would strip down the program until it no longer crashes.



  • @Christian-Ehrlicher

    That is exactly what I am doing :)

    Hopefully I will have a very stripped down version of the program I can test soon.

    Thanks for all your help!


Log in to reply