Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Memory usage increasing continuously: QNetworkReply::realAll()



  • I am implementing a UI to read out internet JSON data every second.
    The usage of memory increases continuously with more or less the same amount of data I should get per second.

    But if I add "reply->deleteLater()" after "reply->readAll()" in the SLOT related to "QNetworkReply:: finished(QNetworkReply*))", the usage of memory stays constantly.

    Is there anything wrong of my codes??

    ps: I have been working in few QSerialPort project, the QDevice::readAll() never needs this "deleteLater()".

    *.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    
    #include <QTimer>
    
    #include <QDebug>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
    
        void on_timeOut_timerTest();
        void read_AS_Json(QNetworkReply* reply);
    
    private:
        Ui::MainWindow *ui;
    
        QNetworkAccessManager *qNetManager_test;
        QTimer *qTime_test;
    
    };
    
    #endif // MAINWINDOW_H
    

    *.CPP

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        qNetManager_test=new QNetworkAccessManager(this);
        connect(qNetManager_test, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_AS_Json(QNetworkReply* )));
    
        qTime_test=new QTimer(this);
        connect(qTime_test,SIGNAL(timeout()),this,SLOT(on_timeOut_timerTest()));
        qTime_test->start(1000);                //read JSON every 1sec
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_timeOut_timerTest()
    {
        qNetManager_test->get(QNetworkRequest(QUrl("http://*********************.json")));
    }
    
    void MainWindow:: read_AS_Json(QNetworkReply* reply)
    {
        reply->readAll();
        // to keep the usage staying constantly, I have to add the code below.
        reply->deleteLater();   
    }
    

  • Lifetime Qt Champion

    Hi
    There is nothing wrong as this is by design.

    void MainWindow:: read_AS_Json(QNetworkReply* reply)
    {
    reply->readAll();
    // to keep the usage staying constantly, I have to add the code below.
    reply->deleteLater(); // this you are expceted to do as the QNetworkReply* reply ownership is assigned to you
    }

    For say serial port there is no such reply object and hence no delete later needed.



  • Thanks @mrjj
    you mean I should do this deleterLater() definitely??


  • Lifetime Qt Champion

    @Yangroot
    Yes, as the NAM cannot know when you are done with it so
    it won't try to delete it and hence its something you must do always. (with NAM)
    So yes please do or you have a leak.


  • Qt Champions 2019

    The documentation is also very clear about this....

    "Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function."


  • Lifetime Qt Champion

    @Christian-Ehrlicher
    Thank you , apparently im a lust scroller as i didn't find the exact spot but i was so sure it was mentioned.



  • Thanks all of you!!!


Log in to reply