PDA

View Full Version : A suggestion about QwtPlotLegendItem



Nicho
16th August 2013, 12:07
Hi Uwe,
I am using QWT6.1 - Downloaded at 20130630.
In my application, it's required to refresh the legend item when the data is changed and I found that :

In the updateLegend() of QwtPlotLegendItem, the layoutItems does not be cleared at each time.
So the list length will be longer and longer, and not be equal to the count of actual items.
eg.
updateLegend( plotItem, dataOfSize1 );
updateLegend( plotItem, dataOfSize2 );
updateLegend( plotItem, dataOfSize1 );
then at the third call of updateLegend(), it will crash.

The codes are like below.

Hope this information is useful.
Nicho



void QwtPlotLegendItem::updateLegend( const QwtPlotItem *plotItem,
const QList<QwtLegendData> &data )
{
if ( plotItem == NULL )
return;

QList<QwtLegendLayoutItem *> layoutItems;

QMap<const QwtPlotItem *, QList<QwtLegendLayoutItem *> >::iterator it =
d_data->map.find( plotItem );
if ( it != d_data->map.end() )
layoutItems = it.value();

bool changed = false;

if ( data.size() != layoutItems.size() )
{
changed = true;

for ( int i = 0; i < layoutItems.size(); i++ )
{
d_data->layout->removeItem( layoutItems[i] );
delete layoutItems[i];//app may crash here.
}
layoutItems.clear();//suggestion
if ( it != d_data->map.end() )
d_data->map.remove( plotItem );

if ( !data.isEmpty() )
{
for ( int i = 0; i < data.size(); i++ )
{
QwtLegendLayoutItem *layoutItem =
new QwtLegendLayoutItem( this, plotItem );
d_data->layout->addItem( layoutItem );
layoutItems += layoutItem;
}

d_data->map.insert( plotItem, layoutItems );
}
}

for ( int i = 0; i < data.size(); i++ )
{
if ( layoutItems[i]->data().values() != data[i].values() )
{
layoutItems[i]->setData( data[i] );
changed = true;
}
}

if ( changed )
{
d_data->layout->invalidate();
itemChanged();
}
}

Uwe
16th August 2013, 14:49
Your suggestion produces memory leaks avoiding a crash by not deleting the items. But obviously something is wrong with the layout items I can't see from your posting.

Could you please upload a minimal compilable demo, where the problem happens ?

Uwe

Nicho
17th August 2013, 03:32
Your suggestion produces memory leaks avoiding a crash by not deleting the items. But obviously something is wrong with the layout items I can't see from your posting.

Could you please upload a minimal compilable demo, where the problem happens ?

Uwe


Uwe,
I am home now for the weekend.
I will try to show you a demo on next Monday.
I am not sure why Uwe think my suggestion will produce memory leak.
What I mean is :
1.keep the line below as it was. So the memory is released.


delete legendItems[i ];//app may crash here

2.add the line below to make the list length back to be zero .
Because after the delete, the pointers in the list are still there but illegal.


legendItems.clear();//suggestion


Nicho

atrofimov
7th April 2015, 10:40
Affirmative.
When is planned to correct this error?

Uwe
7th April 2015, 12:06
Got lost as there was never been the demo code. But anyway - bug is obvious and is fixed now in SVN ( all branches ).

Uwe