SIGSEGV (Segmentation fault) - Bad Pointer i guess



  • Hi I have made a code for a system, an when I try to run it it crashes and comes with the code
    Signal name: SIGSEGV
    Signal meaning: Segmentation fault

    I think that it is because I am using a pointer incorrect, but cant figure out why it is wrong

    I have made a pointer in a class and want to use it in my mainwindow, and I have made a pointer in my mainwindow which i want to use in my class aswell. I know thats abit tricky.

    here is my code.
    @
    #ifndef SERIALSETUP_H
    #define SERIALSETUP_H
    #include <QMessageBox>
    #include <QDebug>
    #include <QSerialPort>

    #include "mainwindow.h"

    class SerialSetup
    {
    public:
    SerialSetup(MainWindow *myWindowPtr);
    void setupSerial();

    private:
    MainWindow *myWindowPtr_;
    QSerialPort *serialPtr_;
    };

    #endif // SERIALSETUP_H

    @

    @

    #include "serialsetup.h"

    SerialSetup::SerialSetup(MainWindow *myWindowPtr)
    {
    myWindowPtr_ = myWindowPtr;
    }

    void SerialSetup::setupSerial()
    {
    serialPtr_ = new QSerialPort(myWindowPtr_); // this is where it goes wrong!
    serialPtr_ ->setPortName("COM2");

    if(serialPtr_->open(QIODevice::ReadWrite))
    {
        qDebug("Serial Opened");
        QMessageBox::information(myWindowPtr_,myWindowPtr_->tr("Succesfull"),myWindowPtr_->tr("Succesfully connected to STK"));
        serialPtr_->setBaudRate(QSerialPort::Baud9600);
        serialPtr_->setDataBits(QSerialPort::Data8);
        serialPtr_->setParity(QSerialPort::NoParity);
        serialPtr_->setStopBits(QSerialPort::OneStop);
        serialPtr_->setFlowControl(QSerialPort::NoFlowControl);
        myWindowPtr_->connect(serialPtr_,SIGNAL(readyRead()),myWindowPtr_,SLOT(serialReceived()));
    }
    else
    {
        QMessageBox::warning(myWindowPtr_,myWindowPtr_->tr("Error"),myWindowPtr_->tr("Connection failed"));
        qDebug ("Serial NOT Opened");
        qDebug() << "error code = " << serialPtr_->error();
        qDebug() << "error string = " << serialPtr_->errorString();
    }
    

    }
    @

    and my mainwindow

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QSerialPort>
    #include <QTimer>
    #include "modify_scenario.h"

    class SerialSetup;

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0, SerialSetup *setupPtr = 0 );

    ~MainWindow();
    

    private slots:
    void serialReceived();
    void on_Modify_button_clicked();
    void on_On_button_clicked();
    void on_Off_button_clicked();
    void on_Minus_button_clicked();
    void on_Plus_button_clicked();
    void on_Scenario1_button_clicked();
    void on_Scenario2_button_clicked();
    void on_Scenario3_button_clicked();
    void on_Load_button_clicked();
    void fcheckButton1();
    void fcheckButton2();
    void fcheckButton3();
    void fcheckLocked();
    //void serialSetup();
    void timeSetup();
    private:
    Ui::MainWindow *ui;
    Modify_Scenario *modify;
    SerialSetup *setupPtr_;
    QSerialPort *serial1Ptr_;
    QTime *time;
    bool Scenario1Chk, Scenario2Chk, Scenario3Chk, LockedChk,OpenChk;
    int counter;

    };

    #endif // MAINWINDOW_H

    @

    @
    #include "ui_mainwindow.h"
    #include "modify_scenario.h"
    #include "serialsetup.h"

    MainWindow::MainWindow(QWidget *parent, SerialSetup *setupPtr) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

     ui->setupUi(this);
    
     setupPtr_ = setupPtr;
     setupPtr_ ->setupSerial();
    

    @

    can any of you figure out whats wrong ?

    Alex.



  • Where do you allocate SerialSetup?
    Is myWindowPtr_ a valid pointer at this line?
    @
    serialPtr_ = new QSerialPort(myWindowPtr_);
    @

    There is no any threads except main GUI thread in your app, right?



  • I think the fact that you are using myWindowPtr_ inside MainWindow constructor makes myWindowPtr_ invalid.



  • Hi Andreyc

    thanks for your response.

    Have made the class SerialSetup to make the MainWindow cpp smaller.

    before I had
    @serialPtr_ = new QSerialPort(this); @

    inside MainWindow, and that worked. now instead of using this pointer I needed to have a pointer to the mainwindow and put it in there. but cant understand why that is wrong.

    dont think I am using the myWindowPtr_ inside MainWindow constructor, though I use it in SerialSetup constructor.

    Alex.



  • Could you show where do you create SerialSetup object?



  • ahh I forgot to initialize the constructor, it never got in to it.!
    figured it out ;)

    thanks.!



  • Glad you solved it.
    Could you put [SOLVED] in the title of you message. Thanks.


Log in to reply
 

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