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

How do I free memory after using QNetworkAccessManager::connectToHost



  • 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();
    }
    


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


  • Lifetime Qt Champion

    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.


Log in to reply