我一直在努力应对Valgrind的信息。我正在使用取自Qt示例(FlowLayout)的QLayout,并在运行时添加,当用户按下添加按钮时,此布局会包含一些小部件。这是继承了我的容器QWidget类的构造函数中的初始化代码段:
// scroll area
auto* central = new QWidget();
central->setObjectName("CentralWidget");
m_layout = new FlowLayout;
ui->scrollArea_step->setFrameShape(QFrame::NoFrame);
central->setLayout(m_layout);
ui->scrollArea_step->setWidgetResizable(true);
ui->scrollArea_step->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea_step->setWidget(central);
我已经了解到,由于有了setWidget(central),我不需要为“ central”小部件分配父对象。
当用户单击界面中的添加按钮时,程序将调用此代码段:
// create the button from rf data
ShortMenuPushButton* stepButton = _createStepButton(m_rfData);
m_layout->addWidget(stepButton);
QPushButton* deleteStepButton = _createDeleteButton();
m_layout->addWidget(deleteStepButton);
_ createStepButton具有以下代码:
auto* stepButton = new ShortMenuPushButton(this);
对于另一个按钮,相同的操作由_createDeleteButton()完成。
如果运行Valgrind,则会收到相同的消息:
==59123== 176 bytes in 2 blocks are definitely lost in loss record 8,040 of 8,630
==59123== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==59123== by 0x6430849: QLayoutPrivate::createWidgetItem(QLayout const*, QWidget*) (qlayout.cpp:200)
==59123== by 0x6431AA0: QLayout::addWidget(QWidget*) (qlayout.cpp:236)
==59123== by 0x7DA967: Dialogs::Dialog_multistep::onButtonAddStep_clicked() (dialog_multistep.cpp:419)
==59123== by 0x7E8B13: QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, void (Dialogs::Dialog_multistep::*)()>::call(void (Dialogs::Dialog_multistep::*)(), Dialogs::Dialog_multistep*, void**) (qobjectdefs_impl.h:152)
==59123== by 0x7E8A87: void QtPrivate::FunctionPointer<void (Dialogs::Dialog_multistep::*)()>::call<QtPrivate::List<>, void>(void (Dialogs::Dialog_multistep::*)(), Dialogs::Dialog_multistep*, void**) (qobjectdefs_impl.h:185)
==59123== by 0x7E89B4: QtPrivate::QSlotObject<void (Dialogs::Dialog_multistep::*)(), QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (qobjectdefs_impl.h:414)
==59123== by 0x9284CB5: call (qobjectdefs_impl.h:394)
==59123== by 0x9284CB5: QMetaObject::activate(QObject*, int, int, void**) (qobject.cpp:3774)
==59123== by 0x64FAFF1: QAbstractButton::clicked(bool) (moc_qabstractbutton.cpp:312)
==59123== by 0x64FB1F3: QAbstractButtonPrivate::emitClicked() (qabstractbutton.cpp:414)
==59123== by 0x64FCD8D: QAbstractButtonPrivate::click() (qabstractbutton.cpp:407)
==59123== by 0x64FCEE4: QAbstractButton::mouseReleaseEvent(QMouseEvent*) (qabstractbutton.cpp:1011)
但是在FlowLayout中,我的析构函数具有:
void FlowLayout::clear()
{
QLayoutItem* item;
while ((item = takeAt(0)))
{
delete item->widget();
}
}
我不知道泄漏在哪里。
我感谢任何建议。谢谢
我发现了问题。如我所写,我使用了FlowLayout类,该类取自一个著名的示例(Qt)。该方法清除确实:
void FlowLayout::clear()
{
QLayoutItem* item;
while ((item = takeAt(0)))
{
delete item->widget();
}
}
但是QLayoutItem在方法QLayoutItem :: widget()中返回nullptr。
也许因为QLayoutItem是基类,应该派生。