![](/img/trans.png)
[英]QT Application segmentation fault on exit when using std::unique_ptr for the QMainWindow
[英]Problem when dereferencing std::unique_ptr with QT (Segmentation fault)
(對不起我未來的錯誤,我是fr)
我正在創建一個程序來用 c++ 中的手機控制電腦( https://github.com/titicplusplus/PCPhone )
服務器部分運行良好,所以我想為用戶添加圖形接口,我決定使用 QT。
當我停止程序 QT 時,出現分段錯誤,這會停止服務器。
我運行圖形界面的function:
void start_g(int argc, char **argv)
{
QApplication app(argc,argv);
QFile File(":/stylesheet.qss");
File.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(File.readAll());
app.setStyleSheet(StyleSheet);
ui interface; //QWidget window
interface.start();
std::cout << "start gui" << std::endl;
interface.show();
app.exec(); //return 0
return;
}
我將代碼封裝在 function 中,因為我想在線程中運行它,但目前在測試期間,我不這樣做。
這是我的 class ui(用戶界面)
class ui : public QWidget
{
public:
ui();
void start();
void change_file();
void change_port();
void freset();
void fapply();
~ui(); //Here is the problem
private:
openf f_open; //to open the extension s file
std::unique_ptr<QFormLayout> formLayout;
std::unique_ptr<QGroupBox> settings;
std::unique_ptr<QVBoxLayout> main_layout;
std::unique_ptr<QGridLayout> grid;
std::vector< std::vector< std::unique_ptr<QPushButton> > > tab2d;
std::unique_ptr<QGroupBox> all_button;
std::unique_ptr<QSpinBox> port_l;
std::unique_ptr<QPushButton> button_f;
std::unique_ptr<QPushButton> apply;
std::unique_ptr<QPushButton> reset;
std::unique_ptr<QLabel> text_f;
};
和重要的 function
ui::ui() {}
void ui::start()
{
grid = std::make_unique<QGridLayout>( this );
/** Code that you don't need, I guess **/
std::cout << "setting part" << std::endl;
all_button = std::make_unique<QGroupBox>("Slide");
all_button->setLayout(grid.get());
port_l =std::make_unique<QSpinBox>(this);
port_l->setMaximum(65535);
port_l->setValue( config_json["port"].get<int>() );
button_f = std::make_unique<QPushButton>("Open Image" ,this);
text_f = std::make_unique<QLabel>(QString::fromStdString(fs::absolute(config_json["image"].get<std::string>())), this);
apply = std::make_unique<QPushButton>(" Apply " ,this);
reset = std::make_unique<QPushButton>("Reset" ,this);
//connect(button_f.get(), &QPushButton::clicked, this, &ui::change_file);
connect(port_l.get(), QOverload<int>::of(&QSpinBox::valueChanged), this, &ui::change_port);
connect(apply.get(), &QPushButton::clicked, this, &ui::fapply);
connect(reset.get(), &QPushButton::clicked, this, &ui::freset);
//auto fileName = QFileDialog::getOpenFileName(&window, "Open Image", "/home/$USER", ("Image Files (*.png *.jpg *.bmp)"));
formLayout = std::make_unique<QFormLayout>();
formLayout->addRow("The port", port_l.get());
formLayout->addRow(text_f.get(), button_f.get()); //////////The problem is here
//formLayout->addRow(apply.get(), reset.get());
settings = std::make_unique<QGroupBox>("Settings");
settings->setLayout(formLayout.get());
main_layout = std::make_unique<QVBoxLayout>(this);
main_layout->addWidget(all_button.get());
main_layout->addWidget(settings.get());
}
所以我在谷歌上看到,問題出在指針上,它們破壞了,所以我試試這個
ui::~ui()
{
main_layout = nullptr;
settings = nullptr;
all_button = nullptr;
std::cout << "1u" << std::endl;
button_f = nullptr;
std::cout << "2u" << std::endl;
text_f = nullptr;
std::cout << "3u" << std::endl;
apply = nullptr;
std::cout << "4u" << std::endl;
reset = nullptr;
std::cout << "5u" << std::endl;
grid = nullptr;
std::cout << "bye bye" << std::endl;
}
結果是[錯誤是法語,所以我嘗試翻譯它]:
1u
Segmentation fault (core dumped)
當我刪除這一行時formLayout->addRow(text_f.get(), button_f.get());
text_f 和 button_f 的順從沒有問題,但是下一個(應用和重置)。
1u
2u
3u
Segmentation fault (core dumped)
在谷歌上,我看到很多時候人們使用指針而不是 std::unique_ptr 和 qt,但我通常看到人們說“智能指針比普通指針好。另外,我嘗試替換button_f.get()
到std::move(button_f).get()
但這不起作用。
有關信息,我在 Ubuntu 20.04,使用 g++-9.3 和 qt5。 預先感謝您的回答!
感謝 GM 和 vahancho
我明白為什么我不需要做std::unique_ptr
。
當我學習 C++ 和指針時,有一條規則: write one delete for every new
,或者更好,使用smart pointer
。
但是有了這兩個主題:
https://codereview.stackexchange.com/questions/43189/deleting-pointers-in-qt5/43201
對於 QT object,它是 QT 刪除指針時 ZA2F2ED4FDC4AAB61DZC21。 所以我只需要在原始指針中更改我的所有std::unique_ptr
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.