Anyway, as already said by others, don't mix stack and heap variables like that! It's very dangerous!
Sample of crashing code:
layout->addWidget(&textWidget);
groupBox->setLayout(layout);
qDebug() << textWidget.parent(); // => groupBox
delete groupBox;
QLineEdit textWidget("Salut");
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(&textWidget);
QGroupBox * groupBox = new QGroupBox();
groupBox->setLayout(layout);
qDebug() << textWidget.parent(); // => groupBox
delete groupBox;
To copy to clipboard, switch view to plain text mode
Just before the crash, you see that textWidget.parent() is groupBox, so it's destructor deletes textWidget too, but textWidget is on the stack and the application crashes.
Warning that also a wrong delete can give the same result, also if everything is in the heap!
Sample of crashing code:
layout->addWidget(textWidget);
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox;
delete textWidget;
QLineEdit * textWidget = new QLineEdit("Salut");
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(textWidget);
QGroupBox * groupBox = new QGroupBox();
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox;
delete textWidget;
To copy to clipboard, switch view to plain text mode
This code, instead, is not crashing!
layout->addWidget(textWidget);
qDebug() << textWidget->parent(); // => NULL
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
layout->removeWidget(textWidget);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox;
QLineEdit * textWidget = new QLineEdit("Salut");
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(textWidget);
qDebug() << textWidget->parent(); // => NULL
QGroupBox * groupBox = new QGroupBox();
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
layout->removeWidget(textWidget);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox;
To copy to clipboard, switch view to plain text mode
See the comments for the output of qDebug(). Note how QHBoxLayout (add and remove functions) does not change the parent of textWidget.
In case of doubt, there is a simple rule: use only objects in the heap and store their pointers with a QPointer (it's always better to understand what you are doing, but this may help if you can't find what is crashing or if you have a shared object):
layout->addWidget(textWidget);
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox; // this deletes textWidget and sets the QPointer to NULL
delete textWidget; // textWidget now is NULL so no crash
QPointer< QLineEdit > textWidget = new QLineEdit("Salut");
QPointer< QHBoxLayout > layout = new QHBoxLayout();
layout->addWidget(textWidget);
QPointer< QGroupBox > groupBox = new QGroupBox();
groupBox->setLayout(layout);
qDebug() << textWidget->parent(); // => groupBox
delete groupBox; // this deletes textWidget and sets the QPointer to NULL
delete textWidget; // textWidget now is NULL so no crash
To copy to clipboard, switch view to plain text mode
A QPointer becomes NULL when it's object is deleted (works only with QObjects).
And remember you don't need to write code like this:
if (ptr != 0) delete ptr;
if (ptr != 0) delete ptr;
To copy to clipboard, switch view to plain text mode
because
delete 0
delete 0
To copy to clipboard, switch view to plain text mode
is valid code!
Bookmarks