Solved Segmentation fault(core dumped) occured after add an private member in widget class.
-
I am building a tool with QT 5,12 to run in the arm64 board. It run well when there was only one private member. When I add more private members, it can compile successfully, but run to the segmentation fault.
QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); QLCDNumber *rebootTimeLcdNum; QTimer *mtimer; private: Ui::Widget *ui; QPushButton *startButton; //QLCDNumber *rebootTimeLcdNum; //QTimer *mtimer; //int a; private slots: void on_startButton_clicked(); void onTimerTimeout(); };
Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); connect(startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked())); }
I use gdb to debug and find that the fault occurred when connecting the slot function on_startButton_clicked() . And the fault won't occur if I put the member to public.
Does anyone experencd the same problem?
-
@Hupe You did not assign a valid pointer to startButton
-
Thanks! Is there any advice on how to assign a valid pointer or some textbooks to refer to?
-
@Hupe
You don't need a textbook to refer to, you need to assign yourstartButton
to whatever button you have which you are wanting to attach the signal/slot to! -
@Hupe This belongs to absolute basics of C++. Do you know how memory is allocated in C++ using "new"?
-
I found setupUi(QWidget *Widget) in ui_widget.h has already new the button. And it works well if there is only one private member *startButton.
-
@Hupe
You need to attach the signal to the button defined in the Designer. But it sounds/looks like you are declaring some newQPushButton *startButton;
in yourWidget
class, which is not the one from Designer? Why are you doing that? -
Thanks! I found the problem.