I am developing a server application that receives commands from client applications through TCP. To create the server, I derived a class called MyTcpServer from QTcpServer and in this class I reimplemented the QTcpServer::incomingConnection( int socketDescriptor ) method so that it creates a MyTcpSocket (derived from QTcpSocket) and adds a pointer to this object to a QList. The reason I store a list of all the MyTcpSockets is that I sometimes need to send a message to all clients.

Here's the significant part of my MyTcpServer class:
Qt Code:
  1. class MyTcpServer : public QTcpServer
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6.  
  7. MyTcpServer( QObject* parent, const QHostAddress& address, quint16 port )
  8. : QTcpServer( parent )
  9. {
  10. listen( address, port );
  11. }
  12.  
  13. QList<MyTcpConnection*> clients()
  14. {
  15. return _clients;
  16. }
  17.  
  18. protected:
  19.  
  20. void incomingConnection( int socketDescriptor )
  21. {
  22. _clients.append( new MyTcpConnection( this, socketDescriptor ) );
  23. }
  24.  
  25. private:
  26.  
  27. QList<MyTcpConnection*> _clients;
  28.  
  29. };
To copy to clipboard, switch view to plain text mode 

This part seems to work perfectly, but if someone sees something wrong, feel free to say it to me.

The part that doesn't work well is when a client disconnects. Like the fortuneserver/client example, I connected the connectionClosed() signal to the deleteLater() slot, but I also need to delete the pointer from the _clients list in the MyTcpServer class. To do that, I store a pointer to the MyTcpServer so I can get the clients list and delete the pointer to the MyTcpConnection using QList::removeAll( const T& value ).

The significant code:
Qt Code:
  1. class MyTcpConnection : public QTcpSocket
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6.  
  7. MyTcpConnection( MyTcpServer* parent, int sock )
  8. : QTcpSocket( static_cast<QObject*>( parent ) ),
  9. {
  10. _server = parent;
  11. connect( this, SIGNAL( disconnected() ), SLOT( slotOnDisconnect() ) );
  12. connect( this, SIGNAL( connectionClosed() ), SLOT( deleteLater() ) );
  13. setSocketDescriptor( sock, QAbstractSocket::ConnectedState, QIODevice::ReadWrite );
  14. }
  15.  
  16. private:
  17.  
  18. MyTcpServer* _server;
  19.  
  20. private slots:
  21.  
  22. void slotOnDisconnect()
  23. {
  24. _server->clients().removeAll( this );
  25. }
  26.  
  27. };
To copy to clipboard, switch view to plain text mode 

The problem is that removeAll doesn't remove the MyTcpConnection pointer from the _clients list!

Making the function a bit more verbose, like this:
Qt Code:
  1. qDebug( "before: %d clients", _server->clients().size() );
  2. int removed = _server->clients().removeAll( this );
  3. qDebug( "%d clients removed", removed );
  4. qDebug( "after: %d clients", _server->clients().size() );
To copy to clipboard, switch view to plain text mode 


...this is what happens:
before: 2 clients
1 clients removed
after: 2 clients

I thought that maybe removeAll didn't really find and removed the client although it returns 1, so I also tried this:
Qt Code:
  1. qDebug( "before: %d clients", _server->clients().size() );
  2. for( int i = 0; i < _server->clients().size(); ++i )
  3. {
  4. MyTcpSocket* client = _server->clients().at(i);
  5. if( client == this )
  6. {
  7. qDebug( "FOUND IT: removing..." );
  8. _server->clients().removeAt(i);
  9. break;
  10. }
  11. else qDebug( "this is not the one" );
  12. }
  13. qDebug( "after: %d clients", _server->clients().size() );
To copy to clipboard, switch view to plain text mode 

...but no luck:
before: 2 clients
FOUND IT: removing...
after: 2 clients

Does anyone know what I'm doing wrong?

P.S. Sorry for the long explanation, but I explained my problem in another place and I got no answers, so I thought trying again here, with more information. I hope the cross-posting isn't a problem as I didn't get a single answer at the other place.