Hi. I would like to ask about your opinion on some kind of problem. I have QTcpServer (working with QSslSockets). There is no threads (except main theadpool). Lets say that the client is trying to connect. Server will take connection and accept it. He will store some client data in containers and perform the necessary connections.

server.h
Qt Code:
  1. class server {
  2. //not significant
  3. protected:
  4. void incomingConnection(int handle);
  5.  
  6. private slots:
  7. void isDisconnected(client *c = 0);
  8. void isReadyRead();
  9. //not significant
  10. };
To copy to clipboard, switch view to plain text mode 

server.cpp
Qt Code:
  1. void server::incomingConnection(int handle) {
  2. //blabla
  3. client *c = new client(this);
  4. if(c->setSocketDescriptor(handle)) {
  5. connect(c, SIGNAL(readyRead()), this, SLOT(isReadyRead()));
  6. connect(c, SIGNAL(disconnected()), this, SLOT(isDisconnected()));
  7. connect(c, SIGNAL(encrypted()), this, SLOT(isEncrypted()));
  8. connect(c, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
  9.  
  10. c->setPrivateKey(privateKey);
  11. c->setLocalCertificate(certificate);
  12. c->startServerEncryption();
  13. } else {
  14. c->deleteLater();
  15. }
  16. }
  17.  
  18. void server::isDisconnected(client *c) {
  19. if(c == 0)
  20. c = static_cast<client*>(sender());
  21.  
  22. //cleaning operations (cleaning containters)
  23.  
  24. //and now:
  25. c->deleteLayer();
  26. }
To copy to clipboard, switch view to plain text mode 

Ok now server is waiting for some data.

server.cpp
Qt Code:
  1. void server::isReadyRead() {
  2. client *c = static_cast<client*>(sender());
  3. buffer = c->readAll();
  4.  
  5. //parsing user data:
  6. if(isLoggedAlready(buffer)) { //maybe client is going to trick me by making multiple connection
  7. client *old = findOldConnection(buffer);
  8. isDisconnected(old); //lets remove his old connection
  9. } else {
  10. //do something else
  11. }
  12. }
To copy to clipboard, switch view to plain text mode 

Is it safe to call this slot (isDisconnected) in such a way ? What if:

a) client will disconnect suddenly (faulty internet connection or ethernet cable has been removed, etc).
b) server still does not know (for a while) that something is wrong with the client.
c) client performs new connection as soon as possible and send some data to him.
d) server will check data (using isLoggedAlready function) and finds that the client is already logged in, so he must remove old connection.

What if somewhere in the events loop, between these steps server has placed information about broken connection ? Is it possible to call isDisconnected slot twice ? it could be dangerous especially when we are releasing memory there.