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. Memory usage increasing continuously: QNetworkReply::realAll()
Forum Updated to NodeBB v4.3 + New Features

Memory usage increasing continuously: QNetworkReply::realAll()

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 476 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.
  • Y Offline
    Y Offline
    Yangroot
    wrote on last edited by
    #1

    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();   
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Y 1 Reply Last reply
      1
      • mrjjM mrjj

        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.

        Y Offline
        Y Offline
        Yangroot
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • Y Yangroot

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          1 Reply Last reply
          3
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            mrjjM 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                Yangroot
                wrote on last edited by
                #7

                Thanks all of you!!!

                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