Client crash after trying to send a message to a closed server
-
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()); } -
You must not delete an object inside a slot called by this object. Use deleteLater()
-
You must not delete an object inside a slot called by this object. Use deleteLater()
@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 -
@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@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.
-
@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 -
@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.QTcpSocketvoid 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?
-
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?
@Idodoqdo said in Client crash after trying to send a message to a closed server:
Will there be a leak?
No
-
@Idodoqdo said in Client crash after trying to send a message to a closed server:
Will there be a leak?
No