Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting clientsocket in slot for signal disconnected() using deleteLater()

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 973 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ghimansh
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • G ghimansh

      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

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        ghimansh
        wrote on last edited by
        #3

        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);
        }
        

        }

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved