Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    SIGSEGV (Segmentation fault) - Bad Pointer i guess

    General and Desktop
    2
    7
    1978
    Loading More Posts
    • 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.
    • M
      Matthacker last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • A
        andreyc last edited by

        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?

        1 Reply Last reply Reply Quote 0
        • A
          andreyc last edited by

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

          1 Reply Last reply Reply Quote 0
          • M
            Matthacker last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • A
              andreyc last edited by

              Could you show where do you create SerialSetup object?

              1 Reply Last reply Reply Quote 0
              • M
                Matthacker last edited by

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

                thanks.!

                1 Reply Last reply Reply Quote 0
                • A
                  andreyc last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post