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. How to use QNetworkAccessManager without leaking memory.
Forum Updated to NodeBB v4.3 + New Features

How to use QNetworkAccessManager without leaking memory.

Scheduled Pinned Locked Moved Solved General and Desktop
37 Posts 8 Posters 5.3k Views 2 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.
  • Q Q139

    @Christian-Ehrlicher Then its solved, i tryed to bodge together networking by not returning from the while loop.
    Assumption was that QApplication.processEvents() runs all functions of event loop including deletions.
    It was for temprorary app i bodged together to collect data via certain api, but there is alot to get and many requests eat memory up.
    Even bodging stuff together requires correct knowledge of how underliying functions works...

    Is there a way to force deletion events from the while(1) event loop?

    void Form::httpFinished()
    {
        disconnect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
        delete reply;
        finished=1;
     //  reply=NULL;
    }
    

    using delete reply; still grows memory

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #15

    @Q139
    Not knowing much about electricity, your picture above is how I power my PCs. Is there anything wrong with this setup?

    Q 1 Reply Last reply
    1
    • JonBJ JonB

      @Q139
      Not knowing much about electricity, your picture above is how I power my PCs. Is there anything wrong with this setup?

      Q Offline
      Q Offline
      Q139
      wrote on last edited by Q139
      #16

      @JonB
      For temporary solution i think its great way to power computer/s if you really needed the power and had no access to better options.

      One possible bad scenario is ,if you add more PCs and increase load at some point it may act as fuse, after breaking connection short circuit and consequently breaking main fuse of the house if reasonable short circuit occurs, or just catching fire.

      Maybe something corrodes over the years and starts to heat, then you will get FREE house heater out of it as well in addition to powering pc, works best on wooden house.

      Connections using proper wires:

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #17

        I replace you sleep with timer, does this change anything?

        #include "form.h"
        #include "ui_form.h"
        #include <QtNetwork>
        #include <QUrl>
        #include "QProgressDialog"
        #include "QMessageBox"
        #include "QTextBlock"
        #include "qcoreapplication.h"
        Form::Form(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Form)
        {
            ui->setupUi(this);
        
        #ifndef QT_NO_SSL
            connect(&qnam, &QNetworkAccessManager::sslErrors,
                    this, &Form::sslErrors);
        #endif
        
        }
        
        Form::~Form()
        {
            delete ui;
        }
        
        void Form::on_pushButton_clicked()
        {
            ui->pushButton->hide();
            startRequest();
        }
        
        void Form::startRequest()
        {    
            qnam.clearAccessCache();
            reply = qnam.get(QNetworkRequest(url));
            connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
        }
        
        void Form::httpFinished()
        {
            reply->deleteLater();
            QTimer::singleShot(1000, this, &Form::startRequest);
        }
        
        Q 1 Reply Last reply
        0
        • B Bonnie

          I replace you sleep with timer, does this change anything?

          #include "form.h"
          #include "ui_form.h"
          #include <QtNetwork>
          #include <QUrl>
          #include "QProgressDialog"
          #include "QMessageBox"
          #include "QTextBlock"
          #include "qcoreapplication.h"
          Form::Form(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::Form)
          {
              ui->setupUi(this);
          
          #ifndef QT_NO_SSL
              connect(&qnam, &QNetworkAccessManager::sslErrors,
                      this, &Form::sslErrors);
          #endif
          
          }
          
          Form::~Form()
          {
              delete ui;
          }
          
          void Form::on_pushButton_clicked()
          {
              ui->pushButton->hide();
              startRequest();
          }
          
          void Form::startRequest()
          {    
              qnam.clearAccessCache();
              reply = qnam.get(QNetworkRequest(url));
              connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
          }
          
          void Form::httpFinished()
          {
              reply->deleteLater();
              QTimer::singleShot(1000, this, &Form::startRequest);
          }
          
          Q Offline
          Q Offline
          Q139
          wrote on last edited by Q139
          #18

          @Bonnie Unfortunately not.

          QNetworkAccessManager qnam;
          QNetworkReply *reply;
          
          void Form::startRequest()
          {
              qnam.clearAccessCache();
              reply = qnam.get(QNetworkRequest(QUrl("www.microsoft.com")));
              connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
          }
          
          void Form::httpFinished()
          {
               disconnect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
              reply->deleteLater();
              QTimer::singleShot(1, this, &Form::startRequest);
          }
          

          This sums the core code up..
          If someone plans to use it as ddos code it will probably flood your own memory up instead.

          B Christian EhrlicherC 2 Replies Last reply
          0
          • Q Q139

            @Bonnie Unfortunately not.

            QNetworkAccessManager qnam;
            QNetworkReply *reply;
            
            void Form::startRequest()
            {
                qnam.clearAccessCache();
                reply = qnam.get(QNetworkRequest(QUrl("www.microsoft.com")));
                connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
            }
            
            void Form::httpFinished()
            {
                 disconnect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
                reply->deleteLater();
                QTimer::singleShot(1, this, &Form::startRequest);
            }
            

            This sums the core code up..
            If someone plans to use it as ddos code it will probably flood your own memory up instead.

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #19

            @Q139 Actually, I don't get a increasing memory while testing the last and the previous code of yours.
            I wonder if it needs to be tested with a url that have a big size of reply data...
            I'm using Qt 5.12 though...

            Q 1 Reply Last reply
            0
            • Q Q139

              @Bonnie Unfortunately not.

              QNetworkAccessManager qnam;
              QNetworkReply *reply;
              
              void Form::startRequest()
              {
                  qnam.clearAccessCache();
                  reply = qnam.get(QNetworkRequest(QUrl("www.microsoft.com")));
                  connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
              }
              
              void Form::httpFinished()
              {
                   disconnect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
                  reply->deleteLater();
                  QTimer::singleShot(1, this, &Form::startRequest);
              }
              

              This sums the core code up..
              If someone plans to use it as ddos code it will probably flood your own memory up instead.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #20

              @Q139 said in How to use QNetworkAccessManager without leaking memory.:

              If someone plans to use it as ddos code it will probably flood your own memory up instead.

              No it won't - there is no leak in this code anymore.
              The disconnect() is not needed though.

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

              Q 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                If someone plans to use it as ddos code it will probably flood your own memory up instead.

                No it won't - there is no leak in this code anymore.
                The disconnect() is not needed though.

                Q Offline
                Q Offline
                Q139
                wrote on last edited by Q139
                #21

                I still get leak.
                alt text

                Could you check if application output gives SSL error while connecting to https site?
                I also had to install openSSL in addition to get it working, maybe it is related to that.
                And while tested on ubuntu got no leak , but it had SSL errors in app output.
                Got SSL for win7 and win10 from here, the lite edition, maybe that component causes problems.

                Christian EhrlicherC 1 Reply Last reply
                0
                • B Bonnie

                  @Q139 Actually, I don't get a increasing memory while testing the last and the previous code of yours.
                  I wonder if it needs to be tested with a url that have a big size of reply data...
                  I'm using Qt 5.12 though...

                  Q Offline
                  Q Offline
                  Q139
                  wrote on last edited by
                  #22

                  @Bonnie Used invalid url text , like "test" also and got leak.

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Q139
                    wrote on last edited by Q139
                    #23

                    Install openSSL on Windows 7 or 10 and see if the app below leaks memory.

                    Windows ssl libraries site
                    direct link to download-Win64 OpenSSL v1.1.1g Light

                    Project file

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      Q139
                      wrote on last edited by Q139
                      #24

                      Current solution to use QNetworkAcessManager 50 times:
                      I thought maybe using just once and deleting gives overhead.
                      This way it goes to ~100mb and back to ~20mb all the time, instead of gb+

                      
                      int qnamUses=0;
                      QNetworkAccessManager *qnam=new QNetworkAccessManager;
                      QNetworkReply *reply;
                      
                      void Form::startRequest()
                      {
                          qnamUses++;
                          if(qnamUses>50){
                                  qnam->deleteLater();
                      
                            qnam=new QNetworkAccessManager;
                          }
                      
                          reply = qnam->get(QNetworkRequest(QUrl("www.microsoft.com")));
                          connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
                      }
                      
                      void Form::httpFinished()
                      {
                        //  qnam.clearAccessCache();
                          //qnam->deleteLater();
                          reply->deleteLater();
                          QTimer::singleShot(0, this, &Form::startRequest);
                      }
                      
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • Q Q139

                        Current solution to use QNetworkAcessManager 50 times:
                        I thought maybe using just once and deleting gives overhead.
                        This way it goes to ~100mb and back to ~20mb all the time, instead of gb+

                        
                        int qnamUses=0;
                        QNetworkAccessManager *qnam=new QNetworkAccessManager;
                        QNetworkReply *reply;
                        
                        void Form::startRequest()
                        {
                            qnamUses++;
                            if(qnamUses>50){
                                    qnam->deleteLater();
                        
                              qnam=new QNetworkAccessManager;
                            }
                        
                            reply = qnam->get(QNetworkRequest(QUrl("www.microsoft.com")));
                            connect(reply, &QNetworkReply::finished, this, &Form::httpFinished);
                        }
                        
                        void Form::httpFinished()
                        {
                          //  qnam.clearAccessCache();
                            //qnam->deleteLater();
                            reply->deleteLater();
                            QTimer::singleShot(0, this, &Form::startRequest);
                        }
                        
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #25

                        @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                        qnam=new QNetworkAccessManager;

                        Why do you create QNetworkAccessManager instance for each request? One can handle many requests...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        Q 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                          qnam=new QNetworkAccessManager;

                          Why do you create QNetworkAccessManager instance for each request? One can handle many requests...

                          Q Offline
                          Q Offline
                          Q139
                          wrote on last edited by
                          #26

                          @jsulm For 50 requests , then schedule for deletion. to prevent memory usage growth.
                          Maybe i am using something incorrectly.

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

                            You never reset qnamUses. Therefore after 51 requests, you recreate your qnam object for every request.

                            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
                            • Q Offline
                              Q Offline
                              Q139
                              wrote on last edited by Q139
                              #28

                              Reset was fixed in test.
                              Is there a way to use single instance of QNetworkAccessManager for 10k+ times without drastic memory usage growth?

                              1 Reply Last reply
                              0
                              • Q Q139

                                I still get leak.
                                alt text

                                Could you check if application output gives SSL error while connecting to https site?
                                I also had to install openSSL in addition to get it working, maybe it is related to that.
                                And while tested on ubuntu got no leak , but it had SSL errors in app output.
                                Got SSL for win7 and win10 from here, the lite edition, maybe that component causes problems.

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #29

                                @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                                I still get leak.

                                The Task manager is no tool to measure a memory leak!

                                Is there a way to use single instance of QNetworkAccessManager for 10k+ times without drastic memory usage growth?

                                Yes, simply use it.

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

                                Q 1 Reply Last reply
                                1
                                • Christian EhrlicherC Christian Ehrlicher

                                  @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                                  I still get leak.

                                  The Task manager is no tool to measure a memory leak!

                                  Is there a way to use single instance of QNetworkAccessManager for 10k+ times without drastic memory usage growth?

                                  Yes, simply use it.

                                  Q Offline
                                  Q Offline
                                  Q139
                                  wrote on last edited by Q139
                                  #30

                                  @Christian-Ehrlicher It ends with app filling memory and crashing if simply using.

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • Q Q139

                                    @Christian-Ehrlicher It ends with app filling memory and crashing if simply using.

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #31

                                    @Q139 I still don't have a valid reproducer for this behavior...

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

                                    Q 1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @Q139 I still don't have a valid reproducer for this behavior...

                                      Q Offline
                                      Q Offline
                                      Q139
                                      wrote on last edited by Q139
                                      #32

                                      @Christian-Ehrlicher I tryed and it leaks on win 7 in vmware and win 10 native.
                                      What version of Qt are you running? Did you also intall the openSSL?

                                      This produces problem in my tests. Only solution i find is to delete QNetworkAccessManager after some period and recreate it.
                                      @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                                      Install openSSL on Windows 7 or 10 and see if the app below leaks memory.

                                      Windows ssl libraries site
                                      direct link to download-Win64 OpenSSL v1.1.1g Light

                                      Project file

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

                                        @Q139 said in How to use QNetworkAccessManager without leaking memory.:

                                        Did you also intall the openSSL?

                                        No need for ssl at all since your url ist http.

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

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

                                          ´Please update the wait time between two requests to 100ms and see if the problem still persists. Looks like there is a race condition when the same url is queried very fast. But can't see the main reason for it currently.

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

                                          1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved