[solved] SIGSEGV segmentation fault



  • I have a problem in my chat application ,I use sqlite to save users name and password , and a client /server architecture . The first window "login" is opened corretly but after login when the chat program begin the application is stopped , and the debugger shows this message :
    "l'inférieur a stoppé car il a reçu un signal du système d'exploitation

    Nom du signal : SIGSEGV

    signification du signal Segmentation fault "
    the debugger shows that the problem is on line 30 of "client.cpp" but I can not find the error!



  • this is my code :

    client.cpp:
    @#include "client.h"
    #include "ui_client.h"

    client::client(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::client)
    {
    ui->setupUi(this);
    QPixmap pix("C:/Users/asma/Desktop/mychat/client/pic.png");
    ui->label_4->setPixmap(pix);
    QPixmap pic("C:/Users/asma/Desktop/mychat/client/chatt.png");
    ui->label_6->setPixmap(pic);

    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(readyRead()), this,SLOT(donneesRecues()));
    connect(socket, SIGNAL(connected()), this, SLOT(connecte()));
    connect(socket, SIGNAL(disconnected()), this,SLOT(deconnecte()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
    tailleMessage = 0;
    

    }

    client::~client()
    {
    delete ui;
    }

    void client::on_boutonConnexion_clicked()
    {
    // On annonce sur la fenêtre qu'on est en train de se connecter
    ui-> listeMessages->append(tr("<em>Tentative de connexion en cours...</em>"));
    ui-> boutonConnexion->setEnabled(false);
    socket->abort(); // On désactive les connexions précédentes s'il y en a
    socket->connectToHost(serveurIP->text(), serveurPort->value());
    // On se connecte au serveur demandé
    }

    void client::on_boutonEnvoyer_clicked()
    {
    QByteArray paquet;
    QDataStream out(&paquet, QIODevice::WriteOnly);
    // On prépare le paquet à envoyer
    QString messageAEnvoyer = tr("<strong>") + pseudo->text() +tr("</strong> : ") + message->text();
    out << (quint16) 0;
    out << messageAEnvoyer;
    out.device()->seek(0);
    out << (quint16) (paquet.size() - sizeof(quint16));
    socket->write(paquet); // On envoie le paquet
    ui-> message->clear(); // On vide la zone d'écriture du message
    ui->message->setFocus(); // Et on remet le curseur à l'intérieur
    }

    void client::on_message_returnPressed()
    {
    on_boutonEnvoyer_clicked();
    }
    void client::donneesRecues()
    {
    /* Même principe que lorsque le serveur reçoit un paquet :
    On essaie de récupérer la taille du message
    Une fois qu'on l'a, on attend d'avoir reçu le message entier (en se
    basant sur la taille annoncée tailleMessage)
    */
    QDataStream in(socket);
    if (tailleMessage == 0)
    {
    if (socket->bytesAvailable() < (int)sizeof(quint16))
    return;
    in >> tailleMessage;
    }
    if (socket->bytesAvailable() < tailleMessage)
    return;
    // Si on arrive jusqu'à cette ligne, on peut récupérer lemessage entier
    QString messageRecu;
    in >> messageRecu;
    // On affiche le message sur la zone de Chat
    ui->listeMessages->append(messageRecu);
    // On remet la taille du message à 0 pour pouvoir recevoir de futurs messages
    tailleMessage = 0;
    }
    // Ce slot est appelé lorsque la connexion au serveur a réussi
    void client::connecte()
    {
    ui->listeMessages->append(tr("<em>Connexion réussie !</em>"));
    boutonConnexion->setEnabled(true);
    }
    // Ce slot est appelé lorsqu'on est déconnecté du serveur
    void client::deconnecte()
    {
    ui->listeMessages->append(tr("<em>Déconnecté du serveur</em>"));
    }
    // Ce slot est appelé lorsqu'il y a une erreur
    void client::erreurSocket(QAbstractSocket::SocketError erreur)
    {
    switch(erreur) // On affiche un message différent selon l'erreur qu'on nous indique
    {
    case QAbstractSocket::HostNotFoundError:
    ui->listeMessages->append(tr("<em>ERREUR : le serveur n'apas pu être trouvé. Vérifiez l'IP et le port.</em>"));
    break;
    case QAbstractSocket::ConnectionRefusedError:
    ui->listeMessages->append(tr("<em>ERREUR : le serveur arefusé la connexion. Vérifiez si le programme "serveur" a bien été lancé. Vérifiez aussi l'IP et le port.</em>"));
    break;
    case QAbstractSocket::RemoteHostClosedError:
    ui->listeMessages->append(tr("<em>ERREUR : le serveur a coupé la connexion.</em>"));
    break;
    default:
    ui->listeMessages->append(tr("<em>ERREUR : ") + socket->errorString() + tr("</em>"));
    }
    ui->boutonConnexion->setEnabled(true);
    }
    @



  • client.h
    @#ifndef CLIENT_H
    #define CLIENT_H

    #include <QDialog>
    #include<QtNetwork>
    #include<QtWidgets>
    #include"ui_client.h"
    namespace Ui {
    class client;
    }

    class client : public QDialog,private Ui::client
    {
    Q_OBJECT

    public:
    explicit client(QWidget *parent = 0);
    ~client();

    private slots:
    void on_boutonConnexion_clicked();

    void on_boutonEnvoyer_clicked();
    
    void on_message_returnPressed();
    void donneesRecues();
    void connecte();
    void deconnecte();
    void erreurSocket(QAbstractSocket::SocketError erreur);
    

    private:
    Ui::client *ui;
    QTcpSocket *socket; // Représente le serveur
    quint16 tailleMessage;
    };

    #endif // CLIENT_H
    @



  • login.h:
    @#ifndef LOGIN_H
    #define LOGIN_H

    #include <QMainWindow>
    #include<QtSql>
    #include<QDebug>
    #include<QFileInfo>
    #include<QDialog>
    #include"client.h"
    #include"inscription.h"

    namespace Ui {
    class login;
    }

    class login : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit login(QWidget *parent = 0);
    ~login();
    QSqlDatabase mydb;
    bool connOpen()
    {
    mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/sqlite/users.sqlite");
    if(!mydb.open())
    {
    qDebug()<<"failed to open the database";
    return false;
    }
    else
    {
    qDebug()<<"Connected...";
    return true;
    }
    }
    void connClose()
    {
    mydb.close();
    mydb.removeDatabase(QSqlDatabase::defaultConnection);
    }

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::login *ui;
    };

    #endif // LOGIN_H
    @



  • login.cpp:
    @#include "login.h"
    #include "ui_login.h"

    login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login)
    {
    ui->setupUi(this);
    QPixmap pic("C:/Users/asma/Desktop/mychat/client/login.png");
    ui->label_4->setPixmap(pic);

    if(!connOpen())
        ui->label_3->setText("failed to open database");
    else
        ui->label_3->setText("Connected");
    

    }

    login::~login()
    {
    delete ui;
    }

    void login::on_pushButton_clicked()
    {
    QString username, password;
    username = ui->lineEdit_username->text();
    password = ui->lineEdit_password->text();

    if(!connOpen())
    {
        qDebug()<<"failed to open database";
        return;
    }
    connOpen();
    QSqlQuery qry;
    //qry.prepare("select * from users where pseudo = '"+username+"' and mot_de_passe = '"+password+"'");
    qry.prepare("select * from users where pseudo = :username and mot_de_passe = :password");
                qry.bindValue(":username",username);
                qry.bindValue(":password",password);
    if (qry.exec&#40;&#41;)
    {
        int count=0;
        while(qry.next())
        {
            count++;
        }
        if(count==1)
        {
            ui->label_3->setText("username and password are correct");
            connClose();
            this->hide();
            client c;
            c.setModal(true);
            c.exec&#40;&#41;;
        }
        if(count<1)
        {
            ui->label_3->setText("username and password incorrect , veuillez s'inscrir");
            connClose();
            this->hide();
            inscription Inscription;
            Inscription.setModal(true);
            Inscription.exec&#40;&#41;;
        }
    
    }
    else qDebug()<< qry.lastError();
    

    }

    void login::on_pushButton_2_clicked()
    {
    this->hide();
    inscription Inscription;
    Inscription.setModal(true);
    Inscription.exec();
    }
    @



  • Hi,
    As far as I know this error could be caused due to accessing a NULL POINTER of invalid memory.



  • Hi,
    As far as I know this error could be caused due to accessing a NULL POINTER or invalid memory.



  • What are type of listeMessages object? And are you initialize listeMessages?



  • Hi , listeMessages is a QTextBrowser



  • i have changed s.th and now the debugger shows me that the ploblem is on line 33 :
    socket->connectToHost(serveurIP->text(), serveurPort->value());


  • Lifetime Qt Champion

    Hi,

    From the code you posted, neither serveurIP nor serveurPort are initialized



  • serveurIP is a QLineEdit ,serveurPort is a QDableSpinBox : both are inisialised in client.ui


  • Lifetime Qt Champion

    Then, the calls should be

    @socket->connectToHost(ui->serveurIP->text(), ui->serveurPort->value());@



  • it's ok i have solved this problem but now another error appaired it's related with socket i will create another discussion . Thanx everybody for help :)


  • Lifetime Qt Champion

    So what was the problem ?

    Since you solved it, please update the thread title prepending [solved] so other forum users may know a solution has been found :)



  • Hi ,the problem was on: "socket->connectToHost(ui->serveurIP->text(), ui->serveurPort->value()); " on this line I forget to put ui-> as you told me @SGaist , thanx for help.


  • Lifetime Qt Champion

    What is strange is that this would compile, so I guess you have member variables with these names in your class ? If so remove you should remove them


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.