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

[Beginner] Segmentation Faults with simple program



  • Hi,
    I just started learning Qt today, and after extensive searching I can't figure out what I'm doing wrong. I went through the Qt for Beginners tutorial, and started trying to add more buttons but every time I add one more button, it keeps segfaulting. I cannot figure out why.


    window.h


    #ifndef WINDOW_H
    #define WINDOW_H

    #include <QWidget>

    class QPushButton;
    class QApplication;
    //class QProgressBar;
    //class QSlider;
    class Window : public QWidget
    {
    Q_OBJECT
    public:
    explicit Window(QWidget *parent = 0);
    private:
    QPushButton *pow_button, *pulseButton, *rptButton, *buttonInfo, *buttonQuit;
    //QProgressBar *progressBar;
    //QSlider *slider;
    int m_counter;
    private slots:
    void slotButtonClicked(bool checked);
    signals:
    void counterReached();
    public slots:

    };

    #endif // WINDOW_H


    main.cpp


    #include <QApplication>
    #include "window.h"

    int main(int argc, char **argv)
    {
    QApplication app (argc, argv);

    Window window;
    window.show();
    
    return app.exec();
    

    }


    window.cpp


    #include "window.h"
    #include <QApplication>
    #include <QPushButton>
    #include <QProgressBar>
    #include <QSlider>

    Window::Window(QWidget *parent) :
    QWidget(parent)
    {
    //Set size of the window
    setFixedSize(1280, 800);

    //Create and position the button
    pow_button = new QPushButton("Power", this);
    pow_button->setToolTip("Laser Power (W)");
    QFont font ("Arial", 23,1,false);
    pow_button->setFont(font);
    QIcon icon ("//lmshare/users/Elmer Wang/Logos & Templates/lmlogo16r.png");
    pow_button->setIcon(icon);
    pow_button->setGeometry(20, 640, 280, 140);
    pow_button->setCheckable(true);

    m_counter = 0;

    pulseButton = new QPushButton("pulse", this);
    pulseButton->setGeometry(340, 640, 280, 140);

    rptButton = new QPushButton("repeat", this);
    rptButton->setGeometry(660, 640, 280, 140);

    buttonInfo = new QPushButton("Info", this);
    buttonInfo->setGeometry(300, 10, 80, 30);

    buttonQuit = new QPushButton("Quit", this);
    buttonQuit->setGeometry(300, 40, 80, 30);

    // connect(buttonInfo, SIGNAL (clicked()), this, SLOT (QApplication::aboutQt()));
    connect(buttonQuit, SIGNAL (clicked()), QApplication::instance(), SLOT (quit()));
    /*
    //Create a progress bar
    //with the range between 0 and 100 and a starting value of 0
    progressBar = new QProgressBar(this);
    progressBar->setRange(0,100);
    progressBar->setValue(0);
    progressBar->setGeometry(10,10,180,30);

    //Create Horizontal slider
    //with the range between 0 and 100, and a starting value of 0
    slider = new QSlider(this);
    slider->setOrientation(Qt::Horizontal);
    slider->setRange(0,100);
    slider->setValue(0);
    slider->setGeometry(10,40,180,30);
    */
    //Connection
    connect(pow_button, SIGNAL (clicked(bool)),this, SLOT (slotButtonClicked(bool)));
    //Quit program once button is clicked 10 times
    connect(this, SIGNAL (counterReached()), QApplication::instance(), SLOT (quit()));
    //This connection sets the value of the progress bar
    //While the slider's value changes
    //connect(slider, SIGNAL (valueChanged(int)),progressBar, SLOT (setValue(int)));
    }

    void Window::slotButtonClicked(bool checked)
    {
    if (checked) {
    pow_button->setText("Checked");
    }else{
    pow_button->setText("Laser Mechanisms");
    }

    m_counter ++;
    if(m_counter==10) {
        emit counterReached();
    }
    

    }


    At one point when I was adding the Pulse button, it was doing the same segmentation fault issue, but then just resolved itself randomly. No idea why that one fixed itself. So I added the repeat button, and it is causing segmentation faults again. I really have no idea what is causing this. Thanks



  • @ewang
    Someone else may spot your line. But this is a good time to learn how to do this for yourself:

    1. Compile for debug.
    2. Run the program from within Qt Creator via the "Debug", not just "Run" button.
    3. Let it get to the point where you say it "seg faiuts".
    4. At this point the debugger will kick in on the fault. Find the "trace back" or "stack trace" window. There it will show you it's on some line somewhere where the fault has occurred. Look at the stack trace to trace back into the line of code in your own program which is currently being executed.
    5. Look closely and you should be able to spot the issue. Likely a variable having the value nullptr or maybe uninitialized. In the debugger you can look at variables' values.

    Now you are a debugging expert, which is far more useful than any answer someone might give you here! :)



  • @JonB So I've used the debugger after compiling, and it takes me to the line in the window.cpp file where the seg fault is occuring. I went okay, so this must be the problem line, and then commented it out. Then it just seg faults in another place (or at least previously this is what was happening). Currently the addition of the rptButton is the only place it is causing a segmentation fault. When I comment it out, the program runs. However I don't really see what's wrong with my syntax, it is almost exactly the same as the other buttons, just a different location. At first I thought maybe it was on top of another button, but changing the coordinates doesn't matter either.
    I assume the stack trace is this window with the different levels?
    I have C++ experience, I'm just not familiar with OOP things, so I thought there was something I was not doing when declaring or defining a variable. The program itself is pretty simple, so I can't figure out where it is causing problems, especially when everything is almost being called exactly the same and being declared exactly the same.
    The variables on the right mostly show parent as 0x0 and QWidget* as the type. Not sure if this is related. Seems like it might be since you said something has the value of nullptr. Insight into that would be helpful.
    Thanks for your feedback.



  • @ewang
    Staring I can't spot what's wrong, or what you could be doing with rptButton which would cause a seg fault. At this stage, people would copy & paste here their stack trace at the seg fault so that others can look at it....



  • @JonB like this? Thank you for the guidance, I've definitely not used a coding forum before, so I'm not familiar with the common procedures.
    0 ?? 0x93bea14
    1 ?? 0x12e9af4
    2 ?? 0xfde24e
    3 ?? 0xfdde2e
    4 ?? 0x10b10c1
    5 ?? 0x113af43
    6 Window::Window window.cpp 25 0x4018c5
    7 qMain main.cpp 8 0x40163d
    8 WinMain*16 131 0x40318d
    9 main 0x40435d



  • Also just as a side note, if I delete the declaration of rptButton in the windows.h file, and comment out the rptButton line in the CPP file, The program works no problem. But I didn't declare *rptButton any differently than I do the other ones, so this is where I'm at a loss.



  • @ewang
    One thing: make it rebuild your whole project from scratch. I delete all the files in the "debug" directory where it's putting all the files it generates.


Log in to reply