Hi

I'm trying to build a streaming server/client program that accommodates multiple clients. I have to keep track of all of them at any one time to determine which one sent a message as ALL data has to be multiplexed through port 4012.

At the moment I'm allocating a new QTcpSocket object each time a new connection is made and keeping track of them through a QList object, but I keep getting heap corruption errors on destruction. I think it may be because of the wrong allocation of sockets or through the destruction of them.

I've been struggling with this for a while now and it's very frustrating as the errors aren't consistent.

Here's the code when QTcpServer signals newConnection(): (ClientSocket inherits from QTcpSocket)

Qt Code:
  1. void Server::acceptConnection()
  2. {
  3. //Allocate space for new connection
  4. ClientSocket *client = new ClientSocket(this);
  5. client = (ClientSocket*)server->nextPendingConnection();
  6.  
  7. if (!client)
  8. QMessageBox::information(this, tr("Error"), tr("No client pointer allocated"));
  9. //Seach for client in client list
  10. int found = -1;
  11.  
  12. if (!clients.isEmpty()) {
  13. for (int i = 0; i < clients.size(); ++i) {
  14. if (clients.at(i)->peerAddress().toString() == client->peerAddress().toString())
  15. found = i;
  16. }
  17. //If not found in array: Don't add duplicates ie. from same host
  18. if (found == -1) {
  19. clients.append(client);
  20. ++connectionCount;
  21.  
  22. updateConnectionLabel();
  23. connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
  24. connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
  25. clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
  26. clients.last()->setReadBufferSize(READ_BUFFER_MAX);
  27. } else {
  28. //If already connected, add to data client list: Add duplicates to data client array
  29. if (connectedClients < MAX_DATA_CLIENTS) {
  30. dataClients.append(client);
  31. ++connectedClients;
  32. updateConnectionLabel();
  33. connect(dataClients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
  34. }
  35. }
  36. } else {
  37. //Add to array
  38. clients.append(client);
  39. ++connectionCount;
  40.  
  41. updateConnectionLabel();
  42. connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
  43. connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
  44. clients.last()->setReadBufferSize(READ_BUFFER_MAX);
  45. clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
  46. }
  47. }
To copy to clipboard, switch view to plain text mode 

The destruction code at this time (I've tried various methods):

Qt Code:
  1. void Server::clientDisconnected()
  2. {
  3. //Remove all the unconnected sockets from the client list
  4. for (int i = 0; i < clients.size(); ++i) {
  5. if (clients.at(i)->state() == QAbstractSocket::UnconnectedState)
  6. {
  7. clients.at(i)->close();
  8. clients.at(i)->deleteLater();
  9. clients.removeAt(i);
  10.  
  11. --connectionCount;
  12. updateConnectionLabel();
  13. }
  14. }
  15.  
  16. //Remove unconnected data clients from dataclient list
  17. for (int i = 0; i < dataClients.size(); ++i) {
  18. if (dataClients.at(i)->state() == QAbstractSocket::UnconnectedState)
  19. {
  20. dataClients.at(i)->close();
  21. dataClients.at(i)->deleteLater();
  22. dataClients.removeAt(i);
  23.  
  24. --connectedClients;
  25. updateConnectionLabel();
  26. }
  27. }
  28. QMessageBox::information(this, tr("DISCONNECT"), tr("DISCONNECT"));
  29. }
To copy to clipboard, switch view to plain text mode 

Thanks for any suggestions