Deleting clientsocket in slot for signal disconnected() using deleteLater()



  • I have single threaded server which can be connected by multiple clients on SSL socket.

    Initially I was deleting the memory for client socket in SlotForDisconnected() using delete <clientSocket_Object> and setting it to NULL after delete.
    This has introduced one bug in the server application. when I disconnect the client connected to it and again connect it to the server, the server gets restarted.

    Then I read somewhere to use deleteLater() in such cases. After using deleteLater() function, its solved if I am connecting from one client and disconnecting it and connecting it again. But when I concurrently made some connections to this server, disconnected them at the same time. Server is getting restarted in this case, while disconnceting form multiple clients at the same time.

    Please suggest with your valuable views.

    Thanks and Regards
    Himanshu


  • Moderators

    @ghimansh

    Hi and welcome to devnet

    Certainly you need somehow to keep track which of the clients is being closed. Otherwise you get confusion and wrong clients are being closed. However, you need to post at least some principle code explaining what you are doing.
    Crystal ball reading tells me that you are probably just having one routine SlotForDisconnected() for deleting clients, but it is not clear how you know which client to close.



  • based on socket_descriptor , we are distinguishing among the clients.

    Below is the code nippet -

    void CServer::SlotOnForeignSslDisconnected()
    {
    try
    {
    CLogFile::WriteLog("SlotOnForeignSslDisconnected","In",1);

        QSslSocket* disconnectedSocket = (QSslSocket*)(sender());
    
    
        CLogFile::WriteLog("SlotOnForeignSslDisconnected",disconnectedSocket->peerAddress().toString(),1);
        CLogFile::WriteLog("SlotOnForeignSslDisconnected",disconnectedSocket->localAddress().toString(),1);
    
        SendUpdatedResultToAll("PList|UP||0","0",0,1); // It is used to send power status to all.
    
        QList<QSslSocket*> listOfAllConnectedSock = vMapSslSockWidDescptr.values();
    
        int sockFD = vMapSslSockWidDescptr.key(disconnectedSocket);
    
        for(int j=0; j<listOfAllConnectedSock.size(); j++)
        {
            AuthenticatedSslUserInfo userInfo = vmapSslSocktWidAuthnticatdUser.value(listOfAllConnectedSock[j]);
    
            if(userInfo.nSocketDescrptr == sockFD)
            {
                CLogFile::WriteLog("SlotOnForeignSslDisconnected","socketFD exist",2);
    
                if(userInfo.strUserName == "ApiClient")
                    sqlDB->SetSystemInfo(QString::number(userInfo.nDeviceID),"-1","0");
    
                CLogFile::WriteLog("SlotOnForeignSslDisconnected","Set Values",2);
                QSslSocket* sock = vMapSslSockWidDescptr.value(userInfo.nSocketDescrptr);
                CLogFile::WriteLog("SlotOnForeignSslDisconnected","Find Socket",2);
                vmapSslSocktWidAuthnticatdUser.remove(sock);
                CLogFile::WriteLog("SlotOnForeignSslDisconnected","Remove Socket And USer Info",2);
                vMapSslSockWidDescptr.remove(sockFD);
                CLogFile::WriteLog("SlotOnForeignSslDisconnected","Remove Sockt and FD",2);
    
                if(vmapAutoSendRCodeUsersList.size() > 0)
                    vmapAutoSendRCodeUsersList.remove(QString::number(sockFD));
    
                if(vmapAutoSendRCodeUsersList.isEmpty())
                {
                    if(timerForAutoSendRCode != NULL)
                    {
                        timerForAutoSendRCode->stop();
                        timerForAutoSendRCode = NULL;
                    }
    
                    QFile::remove(vstrAutoSendRCode);
                }
            }
        }
    
        CLogFile::WriteLog("SlotOnForeignSslDisconnected","Out",1);
        // ToDo - added by Himanshu G to remove memory leaks on 1st December
        if(disconnectedSocket != NULL){
            CLogFile::WriteLog("SlotOnForeignSslDisconnected","Releasing here ssl_socket Code 777",1);
            disconnectedSocket->deleteLater();
          }
    }
    catch(std::exception& e)
    {
        CLogFile::WriteLog("SlotOnForeignSslDisconnected, exception", e.what(),1);
    }
    

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.