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. QLocalSocket Named pipe
Qt 6.11 is out! See what's new in the release blog

QLocalSocket Named pipe

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k 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.
  • K Offline
    K Offline
    king558
    wrote on last edited by
    #1

    Hello all,

    I am using QLocalSocket and QLocalServer for named pipe in MainWindows as member variables. QLocalSocket for multiple client put into QVector<QLocalSocket*>, it works great. But I still have problem with disconnection. I am using a slot void disconnect and a MainWindow::closeEvent. Is this code below correct? I need the slot disconnect, if other part close the connection. And CloseEvent is my part to close the connection. I got crash in slot disconnect.

    QVector<QLocalSocket*> clients;

    void MainWindow::disconnected() {
        for ( auto& client: this->clients ) {
            if ( client->state() == QLocalSocket::UnconnectedState ) {
                client->deleteLater();
            }
        }
    }
    
    void MainWindow::closeEvent( QCloseEvent *event ) {
        for ( auto& client: this->clients ) {
            if ( client->isValid() ) {
                client->disconnectFromServer();
            }
        }
    
        event->accept();
    }
    
    JonBJ kshegunovK 2 Replies Last reply
    0
    • K king558

      Hello all,

      I am using QLocalSocket and QLocalServer for named pipe in MainWindows as member variables. QLocalSocket for multiple client put into QVector<QLocalSocket*>, it works great. But I still have problem with disconnection. I am using a slot void disconnect and a MainWindow::closeEvent. Is this code below correct? I need the slot disconnect, if other part close the connection. And CloseEvent is my part to close the connection. I got crash in slot disconnect.

      QVector<QLocalSocket*> clients;

      void MainWindow::disconnected() {
          for ( auto& client: this->clients ) {
              if ( client->state() == QLocalSocket::UnconnectedState ) {
                  client->deleteLater();
              }
          }
      }
      
      void MainWindow::closeEvent( QCloseEvent *event ) {
          for ( auto& client: this->clients ) {
              if ( client->isValid() ) {
                  client->disconnectFromServer();
              }
          }
      
          event->accept();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @king558
      Run it to crash in the debugger, and look at the stack trace to verify precisely which line of your code it dies from. I assume that if you comment out the client->deleteLater(); it does not crash?

      K 1 Reply Last reply
      0
      • K king558

        Hello all,

        I am using QLocalSocket and QLocalServer for named pipe in MainWindows as member variables. QLocalSocket for multiple client put into QVector<QLocalSocket*>, it works great. But I still have problem with disconnection. I am using a slot void disconnect and a MainWindow::closeEvent. Is this code below correct? I need the slot disconnect, if other part close the connection. And CloseEvent is my part to close the connection. I got crash in slot disconnect.

        QVector<QLocalSocket*> clients;

        void MainWindow::disconnected() {
            for ( auto& client: this->clients ) {
                if ( client->state() == QLocalSocket::UnconnectedState ) {
                    client->deleteLater();
                }
            }
        }
        
        void MainWindow::closeEvent( QCloseEvent *event ) {
            for ( auto& client: this->clients ) {
                if ( client->isValid() ) {
                    client->disconnectFromServer();
                }
            }
        
            event->accept();
        }
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        At a glance, you don't remove the client from the list so after disconnecting one client you're left with a dangling pointer.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • JonBJ JonB

          @king558
          Run it to crash in the debugger, and look at the stack trace to verify precisely which line of your code it dies from. I assume that if you comment out the client->deleteLater(); it does not crash?

          K Offline
          K Offline
          king558
          wrote on last edited by king558
          #4

          @JonB said in QLocalSocket Named pipe:

          @king558
          Run it to crash in the debugger, and look at the stack trace to verify precisely which line of your code it dies from. I assume that if you comment out the client->deleteLater(); it does not crash?

          Thx for your help

          @kshegunov said in QLocalSocket Named pipe:

          At a glance, you don't remove the client from the list so after disconnecting one client you're left with a dangling pointer.

          Thx also for your help.

          Currently I have a workaround like that. But the design of QLocalSocket slot is bad, if there are not another proper solution available.

          this->index = -1;

          void MainWindow::disconnected() {
              if ( this->index >= 0 ) {
                  const auto& client = this->clients[ this->index ];
                  if ( client->state() == QLocalSocket::UnconnectedState ) {
                      client->deleteLater();
                  }
              } else {
                  for ( const auto& client: this->clients  ) {
                      if ( client->state() == QLocalSocket::UnconnectedState ) {
                          client->deleteLater();
                      }
                  }
              }
          }
          
          void MainWindow::closeEvent( QCloseEvent *event ) {
              for ( const auto& client = this->clients ) {
                  this->index++;
                  if ( client->isValid() ) {
                      client->disconnectFromServer();
                  }
              }
          
              event->accept();
          }
          

          I watched many times in stack windows. in closeEvent is a loop and in slot disconnect, that means disconnectFromServer would cause slot disconnect have been called. client->deleteLater(); would be called multiple times, each client two times. Why do disconnected not have a parameter, it would indicate which client object has send the signal?

          kshegunovK 1 Reply Last reply
          0
          • K king558

            @JonB said in QLocalSocket Named pipe:

            @king558
            Run it to crash in the debugger, and look at the stack trace to verify precisely which line of your code it dies from. I assume that if you comment out the client->deleteLater(); it does not crash?

            Thx for your help

            @kshegunov said in QLocalSocket Named pipe:

            At a glance, you don't remove the client from the list so after disconnecting one client you're left with a dangling pointer.

            Thx also for your help.

            Currently I have a workaround like that. But the design of QLocalSocket slot is bad, if there are not another proper solution available.

            this->index = -1;

            void MainWindow::disconnected() {
                if ( this->index >= 0 ) {
                    const auto& client = this->clients[ this->index ];
                    if ( client->state() == QLocalSocket::UnconnectedState ) {
                        client->deleteLater();
                    }
                } else {
                    for ( const auto& client: this->clients  ) {
                        if ( client->state() == QLocalSocket::UnconnectedState ) {
                            client->deleteLater();
                        }
                    }
                }
            }
            
            void MainWindow::closeEvent( QCloseEvent *event ) {
                for ( const auto& client = this->clients ) {
                    this->index++;
                    if ( client->isValid() ) {
                        client->disconnectFromServer();
                    }
                }
            
                event->accept();
            }
            

            I watched many times in stack windows. in closeEvent is a loop and in slot disconnect, that means disconnectFromServer would cause slot disconnect have been called. client->deleteLater(); would be called multiple times, each client two times. Why do disconnected not have a parameter, it would indicate which client object has send the signal?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @king558 said in QLocalSocket Named pipe:

            Why do disconnected not have a parameter, it would indicate which client object has send the signal?

            Usually it isn't needed. However there's an easy solution:

            void MainWindow::disconnected(QLocalSocket * socket)
            {
                // Remove from list and deleteLater()
            }
            
            // Where you create the socket:
            QLocalSocket * socket;
            // Init socket etc.
            QObject::connect(socket, &QLocalSocket::disconnected, mainWindow, std::bind(&MainWindow::disconnected, mainWindow, socket));
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • K king558 has marked this topic as solved on

            • Login

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