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. Client crash after trying to send a message to a closed server
Forum Updated to NodeBB v4.3 + New Features

Client crash after trying to send a message to a closed server

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 824 Views
  • 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.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by
    #1

    The client connects to the server if it exists. Works well with him. But after shutting down the server, when trying to send a message, the client crashes. I'm using SLOT - &QTcpSocket::deleteLater which should logically delete this socket, but I don't understand how it works. If I try to remove the socket myself, after shutting down the server like this:

    {
    ...
    connect(socket, &QTcpSocket::disconnected, this, &MainWindow::disconnect);
    ...
    }
    
    void MainWindow::disconnect() {
    delete socket;
    socket == nullptr;
    }
    

    The client immediately crashes
    Here is the code:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        socket = nullptr;
        connect(ui->ConnectToServer, SIGNAL(released()), this, SLOT(Connect()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::Connect()
    {
        if (socket == nullptr) {
        socket = new QTcpSocket(this);
        connect(socket, &QTcpSocket::readyRead, this, &MainWindow::slotReadyRead);
        connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
        qDebug() << "press connect";
        next_block_size_ = 0;
        socket->connectToHost("127.0.0.1", 2323);
        if(!socket->waitForConnected(5000))
        {
    
            qDebug() << "Error: " << socket->errorString();
            delete socket;
            socket = nullptr;
        }
        }
    
    }
    
    void MainWindow::SendToServer(QString str)
    {
        if (socket != nullptr) {
        data_.clear();
        QDataStream out(&data_, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_6_2);
        out << quint16(0) << str;
        out.device()->seek(0);
        out.device()->seek(data_.size() - sizeof(quint16));
        socket->write(data_);
        ui->lineEdit->clear();
        }
    }
    
    void MainWindow::slotReadyRead()
    {
        QDataStream in (socket);
        in.setVersion(QDataStream::Qt_6_2);
        if (in.status() == QDataStream::Ok) {
            while(1) {
                if (next_block_size_ == 0) {
                    if (socket->bytesAvailable() < 2) {
                        break;
                    }
                    in >> next_block_size_;
                }
                if (socket->bytesAvailable() < next_block_size_) {
                    break;
                }
                QString str;
                in >> str;
                next_block_size_ = 0;
                ui->textBrowser->append(str);
            }
        } else {
            ui->textBrowser->append("Error read");
        }
    }
    
    
    void MainWindow::on_pushButton_2_clicked()
    {
        SendToServer(ui->lineEdit->text());
    }
    
    
    void MainWindow::on_lineEdit_returnPressed()
    {
        SendToServer(ui->lineEdit->text());
    }
    
    1 Reply Last reply
    0
    • I Offline
      I Offline
      Idodoqdo
      wrote on last edited by
      #2
      void MainWindow::disconnect() {
      delete socket;
      socket = nullptr;
      }
      

      misprint

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        You must not delete an object inside a slot called by this object. Use deleteLater()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        I 1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          You must not delete an object inside a slot called by this object. Use deleteLater()

          I Offline
          I Offline
          Idodoqdo
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          But the problem is that after the server stops working and then restarts, the client cannot connect.
          I want the user to be able to reconnect to the server if it first stops working and then works again

          jsulmJ 1 Reply Last reply
          0
          • I Idodoqdo

            @Christian-Ehrlicher
            But the problem is that after the server stops working and then restarts, the client cannot connect.
            I want the user to be able to reconnect to the server if it first stops working and then works again

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Idodoqdo said in Client crash after trying to send a message to a closed server:

            the client cannot connect

            Then find out why it can't connect.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6
              socket->deleteLater();
              socket = nullptr;
              
              I 1 Reply Last reply
              0
              • B Bonnie
                socket->deleteLater();
                socket = nullptr;
                
                I Offline
                I Offline
                Idodoqdo
                wrote on last edited by
                #7

                @Bonnie Thanks a lot, but will qt understand to drop the "old" connection?

                jsulmJ 1 Reply Last reply
                0
                • I Idodoqdo

                  @Bonnie Thanks a lot, but will qt understand to drop the "old" connection?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Idodoqdo Not sure what you mean, but if you delete the socket the connection is closed.
                  Is documented: https://doc.qt.io/qt-6/qtcpsocket.html#dtor.QTcpSocket

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  I 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Idodoqdo Not sure what you mean, but if you delete the socket the connection is closed.
                    Is documented: https://doc.qt.io/qt-6/qtcpsocket.html#dtor.QTcpSocket

                    I Offline
                    I Offline
                    Idodoqdo
                    wrote on last edited by
                    #9

                    @jsulm

                    void MainWindow::Connect()
                    {
                        if (socket == nullptr) {
                        socket = new QTcpSocket(this);
                        connect(socket, &QTcpSocket::readyRead, this, &MainWindow::slotReadyRead);
                        connect(socket, &QTcpSocket::disconnected, this, &MainWindow::Disconnect);
                        qDebug() << "press connect";
                        next_block_size_ = 0;
                        socket->connectToHost("127.0.0.1", 2323);
                        if(!socket->waitForConnected(5000))
                        {
                    
                            qDebug() << "Error: " << socket->errorString();
                            delete socket;
                            socket = nullptr;
                        }
                        }
                    
                    }
                    
                    void MainWindow::Disconnect()
                    {
                        socket->deleteLater();
                        socket = nullptr;
                    }
                    

                    Now it turns out that after the connection is broken (the server has fallen), the socket is told delete later and null is assigned. But if the server goes up and the user decides to connect again, memory is allotted for that socket and a connection is created. Will there be a leak?

                    jsulmJ 1 Reply Last reply
                    0
                    • I Idodoqdo

                      @jsulm

                      void MainWindow::Connect()
                      {
                          if (socket == nullptr) {
                          socket = new QTcpSocket(this);
                          connect(socket, &QTcpSocket::readyRead, this, &MainWindow::slotReadyRead);
                          connect(socket, &QTcpSocket::disconnected, this, &MainWindow::Disconnect);
                          qDebug() << "press connect";
                          next_block_size_ = 0;
                          socket->connectToHost("127.0.0.1", 2323);
                          if(!socket->waitForConnected(5000))
                          {
                      
                              qDebug() << "Error: " << socket->errorString();
                              delete socket;
                              socket = nullptr;
                          }
                          }
                      
                      }
                      
                      void MainWindow::Disconnect()
                      {
                          socket->deleteLater();
                          socket = nullptr;
                      }
                      

                      Now it turns out that after the connection is broken (the server has fallen), the socket is told delete later and null is assigned. But if the server goes up and the user decides to connect again, memory is allotted for that socket and a connection is created. Will there be a leak?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Idodoqdo said in Client crash after trying to send a message to a closed server:

                      Will there be a leak?

                      No

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      I 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Idodoqdo said in Client crash after trying to send a message to a closed server:

                        Will there be a leak?

                        No

                        I Offline
                        I Offline
                        Idodoqdo
                        wrote on last edited by
                        #11

                        @jsulm
                        Thank you very much master

                        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