This code snippet taken from http://doc.qt.digia.com/qt/qhboxlayout.html#details is, barring some magic I'm not aware of, full of potential memory leaks.
Edit: thanks to Nikos C. for pointing out that exception safety is odd in Qt as seen here: http://doc.qt.digia.com/qt/exceptionsafety.html Therefore to keep the main intent of my question valid I updated the example code:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QMyWidgetThatCanThrow *myWidget = new QMyWidgetThatCanThrow("");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
layout->addWidget(myWidget);
window->setLayout(layout);
window->show();
Only at the very end are the widgets reparanted to the window so any exception before the last two lines will leak everything (assuming in real code the window has a parent).
In similar non Qt code I would use a sentry object to deal with this kind of thing. Is there a best practice way of using a sentry here which other people will understand when looking at my code?
Edit: would this be an acceptable design pattern?
void PopulateWindow(QWidget *window)
{
QWidget sentry; //serves as the intial parent ensuring that all
//widgets are either reparented or deleted
QPushButton *button1 = new QPushButton("One",&sentry);
QPushButton *button2 = new QPushButton("Two",&sentry);
QPushButton *button3 = new QPushButton("Three",&sentry);
QPushButton *button4 = new QPushButton("Four",&sentry);
QPushButton *button5 = new QPushButton("Five",&sentry);
QHBoxLayout *layout = new QHBoxLayout(&sentry);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
} //sentry goes out of scope and deletes anything that was not reparented
using it:
QWidget *window = new QWidget;
PopulateWindow(window);
window->show();
what could go wrong? what is the issue you want to avoid? if you create widgets and there is anything which force you to abort, do like you do in regular C/C++
if(huhOh!!){
delete button1;
delete button2;
....
return false
}
Edit:
Nothing prevents you from doing
void PopulateWindow(QWidget *window){
QPushButton *button1 = new QPushButton("One",window);
QPushButton *button2 = new QPushButton("Two",window);
QPushButton *button3 = new QPushButton("Three",window);
QPushButton *button4 = new QPushButton("Four",window);
QPushButton *button5 = new QPushButton("Five",window);
QHBoxLayout *layout = new QHBoxLayout(window);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
//layout already set
//window->setLayout(layout);
}
In this case you will just delete window