Hello. I am developing the download server that should handle 1000+ simultaneous connections. I noticed some memory issue when there are many connections (well, perhaps, it happens with a small number of connections either, but it's just not that obvious). So I see not all the memory that was allocated while establishing connections is released when they are closed.

Here goes the simple server code. It doesn't send or receive any data.
Qt Code:
  1. #include <QCoreApplication>
  2. #include <QtNetwork>
  3.  
  4. class Server: public QTcpServer
  5. {
  6. Q_OBJECT
  7. public:
  8. Server()
  9. {
  10. this->total = 0;
  11. }
  12.  
  13. protected:
  14. virtual void incomingConnection(int d);
  15.  
  16. private:
  17. int total;
  18.  
  19. private slots:
  20. void handleClientDisconnected();
  21. };
  22.  
  23. void Server::incomingConnection(int d)
  24. {
  25. QSslSocket *sock = new QSslSocket;
  26. if (!sock->setSocketDescriptor(d))
  27. delete sock;
  28.  
  29. qDebug() << "socket " << d << " connected, total: " << ++this->total;
  30.  
  31. Q_ASSERT(QFile::exists(":/ssl/resources/my.crt"));
  32. sock->setLocalCertificate(":/ssl/resources/my.crt");
  33. sock->setPrivateKey(":/ssl/resources/my.key", QSsl::Rsa, QSsl::Pem, "la-la-la");
  34.  
  35. connect(sock, SIGNAL(disconnected()), this, SLOT(handleClientDisconnected()));
  36.  
  37. sock->startServerEncryption();
  38. }
  39.  
  40. void Server::handleClientDisconnected()
  41. {
  42. QSslSocket *sock = static_cast<QSslSocket*>(sender());
  43. qDebug() << "Client " << sock->socketDescriptor() << " disconnected, total: " << --this->total;
  44. sock->deleteLater(); // ! deleting the disconnected socket
  45. }
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49. QCoreApplication a(argc, argv);
  50.  
  51. Server server;
  52. if (server.listen(QHostAddress::Any, 563))
  53. qDebug() << "listen started";
  54. else qDebug() << "listen failed";
  55.  
  56. return a.exec();
  57. }
  58.  
  59. #include "main.moc"
To copy to clipboard, switch view to plain text mode 

This is how client connections are opened:
Qt Code:
  1. QSslSocket* sock[1200];
  2.  
  3. for (int i = 0; i < 500; ++i)
  4. {
  5. sock[i] = new QSslSocket;
  6. sock[i]->connectToHostEncrypted("mytestserver.com", 563);
  7. }
To copy to clipboard, switch view to plain text mode 

Again, now data is sent or received. I was running the test several times establishing and closing 200-500 connections. Now there is around 60 Mb of memory used by the server, although all connections were closed.

What is wrong in my server code, so it leaks much? The leak is bigger when transferring some data.