Important: Please read the 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;
        QObject::connect(&timer,&QTimer::timeout, [&manger](){
        return a.exec();

  • I found the source code,

    void QNetworkAccessManager::connectToHost(const QString &hostName, quint16 port)
    	QUrl url;
    	QNetworkRequest request(url);

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

  • Lifetime Qt Champion


    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