View Full Version : QwtPlot::clear() and ownership of QwtLegend

4th February 2008, 11:31
Hi folks

I have a doubt.

QwtPlot *aQwtPlot = new QwtPlot;
QwtLegend *aQwtLegend = new QwtLegend;

Can we say that here we have a mem leak. i.e. memory allocated for aQwtLegend is not taken back (delete is not called) ?

I did look at this below piece of code. I think I should looking into autoDelete and it's default value.

if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
00172 {
00173 item->attach(NULL);
00174 if ( autoDelete )
00175 delete item;
00176 }

This is valgrind output I got.

==7469== at 0x4005C8C: operator new(unsigned) (vg_replace_malloc.c:163)
==7469== by 0x472A6FC: QLayout::addWidget(QWidget*) (qlayout.cpp:240)
==7469== by 0x444F765: QwtLegend::insert(QwtPlotItem const*, QWidget*) (in /usr/local/qwt/lib/libqwt.so.5.0.0)
==7469== by 0x445E326: QwtPlotItem::updateLegend(QwtLegend*) const (in /usr/local/qwt/lib/libqwt.so.5.0.0)
==7469== by 0x4457F6F: QwtPlotCurve::updateLegend(QwtLegend*) const (in /usr/local/qwt/lib/libqwt.so.5.0.0)
==7469== by 0x4453269: QwtPlot::insertLegend(QwtLegend*, QwtPlot::LegendPosition, double) (in /usr/local/qwt/lib/libqwt.so.5.0.0)

9th February 2008, 13:46
QwtPlot::clear is a legacy method from early Qwt versions, that removes all curves and markes from the plot. Today there are more types of QwtPlotItems, so that this method doesn't make too much sense. Better use plot->detachItems().

Removing plot items from the plot also removes the items from the corresponding legend. But QwtPlot::clear doesn't delete the legend and was never defined to do so.