GNOME Bugzilla – Bug 788457
Memory leak in videowidgettest.cpp
Last modified: 2017-10-05 09:34:34 UTC
Hi qt-gstreamer\tests\manual\videowidgettest.cpp VideoWidgetTest::VideoWidgetTest(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { m_ui.setupUi(this); QButtonGroup *group = new QButtonGroup(this); group->addButton(m_ui.playingButton, QGst::StatePlaying); group->addButton(m_ui.pausedButton, QGst::StatePaused); group->addButton(m_ui.readyButton, QGst::StateReady); group->addButton(m_ui.nullButton, QGst::StateNull); connect(group, SIGNAL(buttonClicked(int)), SLOT(onButtonClicked(int))); m_pipeline = QGst::Pipeline::create(); m_src = QGst::ElementFactory::make("videotestsrc"); if (!m_src) { throw std::runtime_error("Unable to create a videotestsrc"); } m_pipeline->add(m_src); } Here when m_src is not created , then we should free the group element memory. if (!m_src) { delete group; throw std::runtime_error("Unable to create a videotestsrc"); }
I disagree. "group" in this scope is a QButtonGroup, which is a QWidget, which is a QObject and it is constructed with "this" as a parent, therefore it "belongs" to the QObject ancestor instance of this class. When an exception is thrown in a constructor, the destructor of this object is not called, but the destructors of its ancestor classes *are* called, since they have successfully completed their execution already (in other words, the ancestor QObject is a fully constructed instance at this point, therefore it needs to be destructed). Therefore, the QObject destructor of this instance *is* called after "throw", so the QButtonGroup is automatically destroyed, together with all the other widgets that are set up in m_ui.setupUi(this);