I have a
QList<QPointer<MTR::RowBox> > rowBoxes;
QList<QPointer<MTR::RowBox> > rowBoxes;
To copy to clipboard, switch view to plain text mode
that is a private member of my main widget.
I want to store pointers to a few widgets in this list as well as I want to place these widgets in a gridLayout. My concern is connected to possible double deletion of these widgets as first may be done by a parent widget (received by layout) and second may be done by QList.
I receive SIGSEV for this code:
pawnBox = new MTR::PawnBox();
QPointer<MTR::RowBox> rbOne = new MTR::RowBox();
rowBoxes.push_back(rbOne);
QPointer<MTR::RowBox> rb2 = new MTR::RowBox();
rowBoxes.push_back(rb2);
QPointer<MTR::RowBox> rb3 = new MTR::RowBox();
rowBoxes.push_back(rb3);
w1->setFixedSize(50,50);
qgl->addWidget(w1,0,0,1,2);
qgl->addWidget(rbOne,1,0);
qgl->addWidget(pawnBox,1,1,3,1);
qgl->addWidget(rb2,2,0);
qgl->addWidget(rb3,3,0);
gameScreen->setLayout(qgl);
QGridLayout *qgl = new QGridLayout();
pawnBox = new MTR::PawnBox();
QPointer<MTR::RowBox> rbOne = new MTR::RowBox();
rowBoxes.push_back(rbOne);
QPointer<MTR::RowBox> rb2 = new MTR::RowBox();
rowBoxes.push_back(rb2);
QPointer<MTR::RowBox> rb3 = new MTR::RowBox();
rowBoxes.push_back(rb3);
QWidget *w1 = new QWidget();
w1->setFixedSize(50,50);
qgl->addWidget(w1,0,0,1,2);
qgl->addWidget(rbOne,1,0);
qgl->addWidget(pawnBox,1,1,3,1);
qgl->addWidget(rb2,2,0);
qgl->addWidget(rb3,3,0);
gameScreen->setLayout(qgl);
To copy to clipboard, switch view to plain text mode
where MTR::RowBox is a pure subclass of a QWidget.
Backtrace is obvious:
#0 0x080559d6 in QList<QPointer<MTR::RowBox> >::~QList ()
#1 0x08052eb7 in MyWidget::~MyWidget ()
#2 0x0804e89e in main ()
#0 0x080559d6 in QList<QPointer<MTR::RowBox> >::~QList ()
#1 0x08052eb7 in MyWidget::~MyWidget ()
#2 0x0804e89e in main ()
To copy to clipboard, switch view to plain text mode
A quotation from QPointer class documentation:
Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it.
Bookmarks