Qt C++命令行程序出现Segmentation fault (core dumped)问题排查
Let's break down why your code is hitting that frustrating Segmentation fault (core dumped) error and how to fix it—this is a classic pointer mishap that's easy to overlook when you're rushing through layout setup!
The Root Cause
Looking at your dlg.cpp constructor, there's a critical mistake in how you're initializing your button pointers:
- You repeatedly assign all new
QPushButtoninstances tobtn1, overwriting the previous pointer each time. This meansbtn2throughbtn12are never initialized—they're wild pointers pointing to random memory addresses. - When you call
gridlayout->addWidget(btn2, 0, 1)and similar lines for the other uninitialized buttons, your program tries to access invalid memory, which triggers the segmentation fault immediately. - As a side note, this also creates a memory leak: every time you overwrite
btn1, the oldQPushButtoninstance is left floating in memory with no way to deallocate it.
Fixed Code for dlg.cpp
Here's the corrected version of your constructor, where each button pointer gets assigned to its own QPushButton instance:
#include "dlg.h" dlg::dlg(){ // Initialize each button pointer to its own QPushButton object btn1 = new QPushButton("btn1"); btn2 = new QPushButton("btn2"); btn3 = new QPushButton("btn3"); btn4 = new QPushButton("btn4"); btn5 = new QPushButton("btn5"); btn6 = new QPushButton("btn6"); btn7 = new QPushButton("btn7"); btn8 = new QPushButton("btn8"); btn9 = new QPushButton("btn9"); btn10 = new QPushButton("btn10"); btn11 = new QPushButton("btn11"); btn12 = new QPushButton("btn12"); gridlayout = new QGridLayout(this); gridlayout->addWidget(btn1,0,0); gridlayout->addWidget(btn2,0,1); gridlayout->addWidget(btn3,0,2); gridlayout->addWidget(btn4,0,3); gridlayout->addWidget(btn5,1,0); gridlayout->addWidget(btn6,1,1); gridlayout->addWidget(btn7,1,2); gridlayout->addWidget(btn8,1,3); gridlayout->addWidget(btn9,2,0); gridlayout->addWidget(btn10,2,1); gridlayout->addWidget(btn11,2,2); gridlayout->addWidget(btn12,2,3); setLayout(gridlayout); setWindowTitle("* QGridLayout *"); }
Optional: Prevent Future Pointer Issues
To avoid similar mistakes down the line, consider using smart pointers to manage your Qt objects. For example, using std::unique_ptr (from the C++ Standard Library) will handle memory cleanup automatically and prevent accidental pointer overwrites:
- Update
dlg.hto include the smart pointer header and modify your member variables:
#include<QGridLayout> #include<QPushButton> #include<QDialog> #include <memory> // Add this for std::unique_ptr class dlg : public QDialog{ private: std::unique_ptr<Q<QPushButton> btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12; QGridLayout *gridlayout; public: dlg(); };
- Initialize the buttons in
dlg.cppusingstd::make_unique:
btn1 = std::make_unique<QPushButton>("btn1"); btn2 = std::make_unique<QPushButton>("btn2"); // ... repeat for all buttons
Next Steps
After applying the fix, re-run your build commands:
cd /hom/gridlayout/ qmake -project qmake gridlayout.pro make
Your program should now run without the segmentation fault, and all 12 buttons will appear in the grid layout as expected.
内容的提问来源于stack exchange,提问作者Omkar

