这段取自http://doc.qt.digia.com/qt/qhboxlayout.html#details的代码片段,除了一些我不知道的魔法之外,充满了潜在的内存泄漏。
编辑:感谢 Nikos C. 指出异常安全在 Qt 中很奇怪,如下所示:http: //doc.qt.digia.com/qt/exceptionsafety.html因此,为了保持我的问题的主要意图有效,我更新了示例代码:
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();
只有在最后才将小部件重新分配给窗口,因此最后两行之前的任何异常都会泄漏所有内容(假设在实际代码中窗口具有父级)。
在类似的非 Qt 代码中,我会使用哨兵对象来处理这种事情。是否有在这里使用哨兵的最佳实践方法,其他人在查看我的代码时会理解?
编辑:这会是可接受的设计模式吗?
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
使用它:
QWidget *window = new QWidget;
PopulateWindow(window);
window->show();