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 do I free memory after using QNetworkAccessManager::connectToHost
Qt 6.11 is out! See what's new in the release blog

How do I free memory after using QNetworkAccessManager::connectToHost

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

    I tried to using QNetworkAccessManager::connectToHost before actual network request to get a lower network latency, however, I found the memory usage keeps increasing each time when I call connectToHost.

    I didn't find any related docs to free memory after connectToHost, wondering if anyone could help me w.ith that, or is it a bug/memory leak for QNetworkAccessManager?

    Here is the minimal code:

    #include <QCoreApplication>
    
    #include <QNetworkAccessManager>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkAccessManager manger;
        QTimer timer;
        timer.setInterval(100);
        QObject::connect(&timer,&QTimer::timeout, [&manger](){
            manger.connectToHost("http://www.google.com");
        });
        timer.start();
    
    
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      KaoN
      wrote on last edited by
      #2

      I found the source code,

      void QNetworkAccessManager::connectToHost(const QString &hostName, quint16 port)
      {
      	QUrl url;
      	url.setHost(hostName);
      	url.setPort(port);
      	url.setScheme(QLatin1String("preconnect-http"));
      	QNetworkRequest request(url);
      	get(request);
      }
      

      It seems I have to capture the reply after finished and delete manually.

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

        Hi,

        In this case, it might be simpler to implement your own function for connectToHost. From the code here there's no way to get the reply from the get call.

        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

        • Login

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