BottCode
BottCode

Reputation: 31

Segmentation fault in destructor Qt QDialog

When I close the qdialog_search's parent(a mainwindow), the qdebugger evidence a segm fault in the qdialog_search destructor. qdialog_search is derived form QDialog(and maybe this is the problem). this is the code:

//.h
#ifndef QDIALOG_SEARCH_H
#define QDIALOG_SEARCH_H
#include <QDialog>
#include <QVBoxLayout>
#include <QTabWidget>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
class QDialog_search: public QDialog{
private:
QVBoxLayout* main;
QTabWidget* tab;
// id
QWidget* search_id;
QVBoxLayout* id_l;
QLineEdit* id_line;
QPushButton* id_button;
// owner
QWidget* search_owner;
QVBoxLayout* ow_l;
QLineEdit* ow_line;
QPushButton* ow_button;
// destination
QWidget* search_dest;
QVBoxLayout* dest_l;
QLineEdit* dest_line;
QPushButton* dest_button;
public:
QDialog_search(QWidget* parent=0);
void buildSearchId();
void buildSearchOwner();
void buildSearchDest();
~QDialog_search();

};

#endif // QDIALOG_SEARCH_H

//.cpp
          QDialog_search::QDialog_search(QWidget *parent):QDialog(parent),
main(new QVBoxLayout()),
tab(new QTabWidget()),
id_l(new QVBoxLayout()),
search_dest(new     QWidget(tab)),search_id(new QWidget(tab))
,search_owner(new QWidget(tab)),ow_l(new QVBoxLayout()),
dest_l(new   QVBoxLayout()){
setLayout(main);
setWindowTitle("SEARCH");
setWindowIcon(QIcon(":/icon/icone/search.png"));
setMinimumSize(200,200);
main->addWidget(tab);
tab->addTab(search_id,"ID");
tab->addTab(search_dest,"destination");
tab->addTab(search_owner,"owner");
buildSearchId();
buildSearchDest();
buildSearchOwner();
}

void QDialog_search::buildSearchId(){
search_id->setLayout(id_l);
QLabel* id_lab=new QLabel("Search ID",search_id);
id_lab->setMaximumSize(60,30);
id_l->addWidget(id_lab);
id_line=new QLineEdit(search_id);id_line->setMaximumSize(120,30);
id_l->addWidget(id_line);
id_button=new QPushButton(search_id);id_button->setMaximumSize(120,30);
id_button->setIcon(QIcon(":/icon/icone/search.png"));
id_l->addWidget(id_button);
}

void QDialog_search::buildSearchOwner(){
search_owner->setLayout(ow_l);
QLabel* ow_lab=new QLabel("Search  owner",search_owner);
ow_lab->setMaximumSize(90,30);
ow_l->addWidget(ow_lab);
ow_line=new QLineEdit(search_owner);ow_line->setMaximumSize(120,30);
ow_l->addWidget(ow_line);
ow_button=new QPushButton(search_owner);ow_button->setMaximumSize(120,30);
ow_button->setIcon(QIcon(":/icon/icone/search.png"));
ow_l->addWidget(ow_button);
}

void QDialog_search::buildSearchDest(){
search_dest->setLayout(dest_l);
QLabel* dest_lab=new QLabel("Search  destination",search_dest);
dest_lab->setMaximumSize(120,30);
dest_l->addWidget(dest_lab);
dest_line=new QLineEdit(search_dest);dest_line->setMaximumSize(120,30);
dest_l->addWidget(dest_line);
dest_button=new QPushButton(search_dest);
dest_button->setMaximumSize(120,30);
dest_button->setIcon(QIcon(":/icon/icone/search.png"));
dest_l->addWidget(dest_button);
}

QDialog_search::~QDialog_search(){
delete main;
delete tab;
delete search_id; // debugger say this is segm fault, search_id is  //QWidget*
delete id_l;
delete id_line;
delete id_button;
delete search_owner;
delete ow_l;
delete ow_line;
delete ow_button;
delete search_dest;
delete dest_l;
delete dest_line;
delete dest_button;
}

I tried to remove "delete search_id" but debugger evidence a segm fault at the end of ~QDialog_search(). Sometimes, there's a segm fault when I build the QDialog_search, but only sometimes.

Upvotes: 0

Views: 1471

Answers (1)

nwp
nwp

Reputation: 9991

The error stems from a disagreement how you and Qt handle memory. search_id is a child of tab since you set tab as the parent in search_id(new QWidget(tab)). In Qt's view the parent is responsible for deleteing the child. In the destructor you delete tab; which consequently also deletes its child search_id. Then you delete search_id, but it doesn't exist anymore, therefore you get a segmentation fault.

I recommend you read through Qt's Object Trees & Ownership to understand the ownership model better and avoid this type of error in the future.

Upvotes: 3

Related Questions