Avoids iterator invalidation in QwtPlot item lists

When working with a summary ensemble in a Debug build on Windows, a crash happened in RiuQwtPlotWidget::resetPlotCurveHighlighting(). When iterating over list of plot items, one pointer to item pointed to 0xddddddd, a clear indication that the underlying list had been updated during the loop and the iterator invalidated.

The change ensures that modifications to the item list during iteration do not cause unexpected behavior, particularly in scenarios involving grid lines and curve highlighting.

This is achieved by making a copy of the item list before iteration when modifications may occur. For cases where no add/remove will be performed during the iteration, a comment is added to clarify that it is safe to access the reference to the list.
This commit is contained in:
Magne Sjaastad
2025-10-14 12:52:24 +02:00
parent b704f1f607
commit 5f2e2919c8
6 changed files with 14 additions and 2 deletions
@@ -569,6 +569,7 @@ protected:
double minDistance = std::numeric_limits<double>::max();
QString closestCurveLabel;
// Ok to access reference to list, no add/remove of item during iteration
for ( QwtPlotItem* item : plot()->itemList() )
{
if ( item->rtti() == QwtPlotItem::Rtti_PlotCurve )