Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Program exiting when closing QTcpServer and socket with a QPushButton



  • Hello !

    I have a program that communicates via Tcp .(I'm on Qt4.7)
    img_dune.PNG

    This is my class IHM's form that herits from QWidget. When I click on the launch button my program starts, creates a QTcpServer that connects to a distant client and a QTcpSocket connecting to a distant TcpServer and they exchange messages.
    (This is dealt in an other class)

    Then I tried to add a initialize button that would close the sockets and clear the contents of my QTableWidget
    In my IHM.cpp :

    void IHM::on_initialize_clicked()
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("Initialization");
        msgBox.setText("Are you sure you want to (re)initialize DUNE ? ");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        int ret = msgBox.exec();
        msgBox.clickedButton();
        switch(ret)
        {
        case QMessageBox::Yes:
            ui->buttonLaunch->setChecked(false);
            ui->tableWidget->clearContents();
            ui->tableWidget->setRowCount(0);
            ui->tacticalPort->setEnabled(false);
            ui->tacticalIP->setEnabled(false);
            ui->comboBox->setCurrentIndex(0);
    
            if (ui->tableWidget2->isEnabled())
            {
                ui->tableWidget2->clearContents();
                ui->tableWidget2->setRowCount(0);
                ui->tableWidget2->setEnabled(false);
            }
            else {
            }
            if (server->getServerNrs() != NULL)
            {
                server->getServerNrs()->deleteLater();
            }
            else {
            }
            if(server->getSocketTechnique() != NULL)
            {
                server->getSocketTechnique()->close();
                server->getSocketTechnique()->deleteLater();
            }
            else {
            }
            if(server->getSocketTactique() != NULL)
            {
                server->getSocketTactique()->close();
                server->getSocketTactique()->deleteLater();
            }
            else {
            }
            if(server->getSocketServer() != NULL)
            {
                server->getSocketServer()->close();
                server->getSocketServer()->deleteLater();
            }
            else {
            }
            break;
        case QMessageBox::No:
            msgBox.close();
            break;
        }
    }
    

    But then when I click on the yes button on my message box my program closes and i don't know why.

    Here is my main.cpp :

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
       IHM ihm;
       ihm.show();
      return a.exec();
    }
    

    When I tried to debug :
    Sometimes it went throught all my void IHM::on_initialize_clicked() function
    and then went looping on the return a.exec() and exits with a :
    SIGSEGV
    Segmentation fault
    and the other times it exited at the line server->getSocketServer()->deleteLater();with the same error.

    I have no idea why and hope someone could help me. Thanks !



  • 99.9999% probability you are trying to use the object you just deleted.
    If you call deleteLater on an object you then have to recreate it (e.g. with new) before using it or trying to delete it again