Memory Management : Fixes related to plot objects and curves

This commit is contained in:
Magne Sjaastad 2022-02-25 14:47:15 +01:00
parent 47a44a79df
commit 4693dc44ef
14 changed files with 128 additions and 60 deletions

View File

@ -131,7 +131,6 @@ RimPlotCurve::~RimPlotCurve()
{
if ( m_plotCurve )
{
detach();
delete m_plotCurve;
m_plotCurve = nullptr;
}
@ -1017,12 +1016,15 @@ void RimPlotCurve::detach( bool deletePlotCurve )
{
if ( m_plotCurve )
{
m_plotCurve->detach();
if ( deletePlotCurve )
{
delete m_plotCurve;
m_plotCurve = nullptr;
}
else
{
m_plotCurve->detach();
}
}
replotParentPlot();
@ -1033,7 +1035,7 @@ void RimPlotCurve::detach( bool deletePlotCurve )
//--------------------------------------------------------------------------------------------------
void RimPlotCurve::reattach()
{
if ( m_parentPlot ) attach( m_parentPlot );
if ( m_parentPlot && canCurveBeAttached() ) attach( m_parentPlot );
}
//--------------------------------------------------------------------------------------------------
@ -1044,6 +1046,15 @@ bool RimPlotCurve::isSameCurve( const RiuPlotCurve* plotCurve ) const
return m_plotCurve == plotCurve;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimPlotCurve::deletePlotCurve()
{
delete m_plotCurve;
m_plotCurve = nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -126,6 +126,7 @@ public:
void detach( bool deletePlotCurve = false );
void reattach();
bool isSameCurve( const RiuPlotCurve* plotCurve ) const;
void deletePlotCurve();
protected:
virtual QString createCurveAutoName() = 0;

View File

@ -179,7 +179,7 @@ RimSummaryPlot::~RimSummaryPlot()
{
removeMdiWindowFromMdiArea();
cleanupBeforeClose();
deletePlotCurvesAndPlotWidget();
delete m_summaryCurveCollection;
delete m_ensembleCurveSetCollection;
@ -1378,7 +1378,7 @@ void RimSummaryPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
// Destroy viewer
removeMdiWindowFromMdiArea();
cleanupBeforeClose();
deletePlotCurvesAndPlotWidget();
}
#endif
@ -1401,14 +1401,9 @@ void RimSummaryPlot::childFieldChangedByUi( const caf::PdmFieldHandle* changedCh
//--------------------------------------------------------------------------------------------------
void RimSummaryPlot::updateStackedCurveData()
{
for ( RimPlotAxisPropertiesInterface* axisProperties : m_axisProperties )
{
if ( axisProperties->plotAxisType().axis() == RiaDefines::PlotAxis::PLOT_AXIS_LEFT ||
axisProperties->plotAxisType().axis() == RiaDefines::PlotAxis::PLOT_AXIS_RIGHT )
updateStackedCurveDataForAxis( axisProperties->plotAxisType() );
}
auto anyStackedCurvesPresent = updateStackedCurveDataForRelevantAxes();
if ( plotWidget() )
if ( plotWidget() && anyStackedCurvesPresent )
{
reattachAllCurves();
plotWidget()->scheduleReplot();
@ -1418,13 +1413,30 @@ void RimSummaryPlot::updateStackedCurveData()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryPlot::updateStackedCurveDataForAxis( RiuPlotAxis plotAxis )
bool RimSummaryPlot::updateStackedCurveDataForRelevantAxes()
{
std::map<RiaDefines::PhaseType, size_t> curvePhaseCount;
bool anyStackedCurvesPresent = false;
for ( RimPlotAxisPropertiesInterface* axisProperties : m_axisProperties )
{
if ( axisProperties->plotAxisType().axis() == RiaDefines::PlotAxis::PLOT_AXIS_LEFT ||
axisProperties->plotAxisType().axis() == RiaDefines::PlotAxis::PLOT_AXIS_RIGHT )
{
anyStackedCurvesPresent |= updateStackedCurveDataForAxis( axisProperties->plotAxisType() );
}
}
return anyStackedCurvesPresent;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimSummaryPlot::updateStackedCurveDataForAxis( RiuPlotAxis plotAxis )
{
auto stackedCurves = visibleStackedSummaryCurvesForAxis( plotAxis );
if ( stackedCurves.empty() ) return false;
// Reset all curves
std::map<RiaDefines::PhaseType, size_t> curvePhaseCount;
for ( RimSummaryCurve* curve : stackedCurves )
{
// Apply a area filled style if it isn't already set
@ -1473,6 +1485,8 @@ void RimSummaryPlot::updateStackedCurveDataForAxis( RiuPlotAxis plotAxis )
zPos -= 1.0;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
@ -1595,7 +1609,7 @@ void RimSummaryPlot::updateZoomFromParentPlot()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryPlot::cleanupBeforeClose()
void RimSummaryPlot::deletePlotCurvesAndPlotWidget()
{
if ( isDeletable() )
{
@ -1606,6 +1620,8 @@ void RimSummaryPlot::cleanupBeforeClose()
plotWidget()->setParent( nullptr );
}
deleteAllPlotCurves();
if ( m_summaryPlot )
{
m_summaryPlot.reset();
@ -1905,7 +1921,11 @@ void RimSummaryPlot::handleDroppedObjects( const std::vector<caf::PdmObjectHandl
}
}
if ( newCurves > 0 ) applyDefaultCurveAppearances();
if ( newCurves > 0 )
{
applyDefaultCurveAppearances();
loadDataAndUpdate();
}
updateConnectedEditors();
}
@ -1919,7 +1939,6 @@ void RimSummaryPlot::addNewCurveY( const RifEclipseSummaryAddress& address, RimS
newCurve->setSummaryCaseY( summaryCase );
newCurve->setSummaryAddressYAndApplyInterpolation( address );
addCurveNoUpdate( newCurve );
newCurve->loadDataAndUpdate( true );
}
//--------------------------------------------------------------------------------------------------
@ -1996,7 +2015,7 @@ RiuPlotWidget* RimSummaryPlot::doCreatePlotViewWidget( QWidget* mainWindowParent
if ( useQtCharts )
{
m_summaryPlot = std::make_unique<RiuSummaryQtChartsPlot>( this, mainWindowParent );
m_summaryPlot = std::make_unique<RiuSummaryQtChartsPlot>( this );
}
else
{
@ -2044,7 +2063,7 @@ RiuPlotWidget* RimSummaryPlot::doCreatePlotViewWidget( QWidget* mainWindowParent
//--------------------------------------------------------------------------------------------------
void RimSummaryPlot::deleteViewWidget()
{
cleanupBeforeClose();
deletePlotCurvesAndPlotWidget();
}
//--------------------------------------------------------------------------------------------------
@ -2190,6 +2209,17 @@ void RimSummaryPlot::detachAllPlotItems()
m_plotInfoLabel->detach();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryPlot::deleteAllPlotCurves()
{
for ( auto* c : summaryCurves() )
{
c->deletePlotCurve();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -206,6 +206,7 @@ private:
void doUpdateLayout() override;
void detachAllPlotItems();
void deleteAllPlotCurves();
void handleKeyPressEvent( QKeyEvent* keyEvent ) override;
@ -223,9 +224,6 @@ protected:
void onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects ) override;
void updateStackedCurveData();
void updateStackedCurveDataForAxis( RiuPlotAxis plotAxis );
void defineUiTreeOrdering( caf::PdmUiTreeOrdering& uiTreeOrdering, QString uiConfigName = "" ) override;
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
void onLoadDataAndUpdate() override;
@ -249,7 +247,7 @@ private:
void updateTimeAxis( RimSummaryTimeAxisProperties* timeAxisProperties );
void cleanupBeforeClose();
void deletePlotCurvesAndPlotWidget();
void connectCurveSignals( RimSummaryCurve* curve );
void disconnectCurveSignals( RimSummaryCurve* curve );
@ -272,6 +270,10 @@ private:
void addNewCurveY( const RifEclipseSummaryAddress& address, RimSummaryCase* summaryCase );
void updateStackedCurveData();
bool updateStackedCurveDataForAxis( RiuPlotAxis plotAxis );
bool updateStackedCurveDataForRelevantAxes();
private:
#ifdef USE_QTCHARTS
caf::PdmField<bool> m_useQtChartsPlot;

View File

@ -54,7 +54,26 @@ RiuQtChartsPlotCurve::RiuQtChartsPlotCurve( RimPlotCurve* ownerRimCurve, const Q
//--------------------------------------------------------------------------------------------------
RiuQtChartsPlotCurve::~RiuQtChartsPlotCurve()
{
detach();
if ( m_plotWidget && m_plotWidget->qtChart() )
{
auto* line = lineSeries();
if ( line )
{
m_plotWidget->qtChart()->removeSeries( line );
// removeSeries() releases chart ownership of the data, delete data to avoid memory leak
delete line;
}
auto* scatter = scatterSeries();
if ( scatter )
{
m_plotWidget->qtChart()->removeSeries( scatter );
// removeSeries() releases chart ownership of the data, delete data to avoid memory leak
delete scatter;
}
}
// Delete if it is still owned by by plot curve
delete m_lineSeries;
@ -164,8 +183,6 @@ void RiuQtChartsPlotCurve::detach()
}
if ( m_plotWidget ) setVisibleInLegend( false );
m_plotWidget = nullptr;
}
//--------------------------------------------------------------------------------------------------
@ -188,13 +205,15 @@ void RiuQtChartsPlotCurve::setSamplesInPlot( const std::vector<double>& xValues,
QtCharts::QLineSeries* line = lineSeries();
QtCharts::QScatterSeries* scatter = scatterSeries();
line->clear();
scatter->clear();
QVector<QPointF> values( static_cast<int>( xValues.size() ) );
for ( int i = 0; i < static_cast<int>( xValues.size() ); i++ )
{
line->append( xValues[i], yValues[i] );
scatter->append( xValues[i], yValues[i] );
values[i] = QPointF( xValues[i], yValues[i] );
}
line->replace( values );
scatter->replace( values );
}
//--------------------------------------------------------------------------------------------------

View File

@ -90,7 +90,7 @@ private:
QtCharts::QLineSeries* m_lineSeries;
QtCharts::QScatterSeries* m_scatterSeries;
std::shared_ptr<RiuPlotCurveSymbol> m_symbol;
RiuQtChartsPlotWidget* m_plotWidget;
QPointer<RiuQtChartsPlotWidget> m_plotWidget;
RiuPlotAxis m_axisX;
RiuPlotAxis m_axisY;
};

View File

@ -717,8 +717,8 @@ RiuQtChartsPlotWidget::AxisScaleType RiuQtChartsPlotWidget::axisScaleType( RiuPl
//--------------------------------------------------------------------------------------------------
void RiuQtChartsPlotWidget::setAxisScaleType( RiuPlotAxis axis, RiuQtChartsPlotWidget::AxisScaleType axisScaleType )
{
QAbstractAxis* removeaxis = plotAxis( axis );
QAbstractAxis* insertaxis = nullptr;
QAbstractAxis* axisToBeDeleted = plotAxis( axis );
QAbstractAxis* insertaxis = nullptr;
if ( axisScaleType == AxisScaleType::LOGARITHMIC )
{
@ -734,15 +734,19 @@ void RiuQtChartsPlotWidget::setAxisScaleType( RiuPlotAxis axis, RiuQtChartsPlotW
}
QChart* chart = qtChart();
if ( chart->axes().contains( removeaxis ) ) chart->removeAxis( removeaxis );
if ( chart->axes().contains( axisToBeDeleted ) ) chart->removeAxis( axisToBeDeleted );
chart->addAxis( insertaxis, mapPlotAxisToQtAlignment( axis.axis() ) );
m_axes[axis] = insertaxis;
for ( auto serie : chart->series() )
{
if ( serie->attachedAxes().contains( removeaxis ) ) serie->detachAxis( removeaxis );
if ( serie->attachedAxes().contains( axisToBeDeleted ) ) serie->detachAxis( axisToBeDeleted );
serie->attachAxis( insertaxis );
}
// We have the ownership of the axis object, delete to avoid memory leak
delete axisToBeDeleted;
axisToBeDeleted = nullptr;
}
//--------------------------------------------------------------------------------------------------
@ -789,8 +793,6 @@ void RiuQtChartsPlotWidget::attach( RiuPlotCurve* plotCurve,
qtChart()->addSeries( series );
setXAxis( xAxis, series );
setXAxis( yAxis, series );
rescaleAxis( xAxis );
rescaleAxis( yAxis );
}
};

View File

@ -63,12 +63,10 @@ RiuQwtPlotCurve::RiuQwtPlotCurve( RimPlotCurve* ownerRimCurve, const QString& ti
//--------------------------------------------------------------------------------------------------
RiuQwtPlotCurve::~RiuQwtPlotCurve()
{
if ( m_qwtCurveErrorBars )
{
m_qwtCurveErrorBars->detach();
delete m_qwtCurveErrorBars;
m_qwtCurveErrorBars = nullptr;
}
detach();
delete m_qwtCurveErrorBars;
m_qwtCurveErrorBars = nullptr;
}
//--------------------------------------------------------------------------------------------------

View File

@ -37,8 +37,7 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuSummaryPlot::RiuSummaryPlot( RimSummaryPlot* plot, QWidget* parent )
: QWidget( parent )
RiuSummaryPlot::RiuSummaryPlot( RimSummaryPlot* plot )
{
}
@ -54,6 +53,8 @@ RiuSummaryPlot::~RiuSummaryPlot()
//--------------------------------------------------------------------------------------------------
void RiuSummaryPlot::showContextMenu( QPoint pos )
{
if ( !plotWidget() ) return;
QMenu menu;
caf::CmdFeatureMenuBuilder menuBuilder;
@ -66,7 +67,7 @@ void RiuSummaryPlot::showContextMenu( QPoint pos )
if ( plotCurve && closestCurvePoint >= 0 )
{
RimSummaryCurve* summaryCurve = dynamic_cast<RimSummaryCurve*>( plotCurve->ownerRimCurve() );
auto* summaryCurve = dynamic_cast<RimSummaryCurve*>( plotCurve->ownerRimCurve() );
if ( summaryCurve && closestCurvePoint < (int)summaryCurve->timeStepsY().size() )
{
std::time_t timeStep = summaryCurve->timeStepsY()[closestCurvePoint];
@ -105,7 +106,7 @@ void RiuSummaryPlot::showContextMenu( QPoint pos )
if ( !curveClicked )
{
RimSummaryPlot* summaryPlot = static_cast<RimSummaryPlot*>( plotWidget()->plotDefinition() );
auto* summaryPlot = static_cast<RimSummaryPlot*>( plotWidget()->plotDefinition() );
std::vector<RimEnsembleCurveSet*> allCurveSetsInPlot;
summaryPlot->descendantsOfType( allCurveSetsInPlot );
for ( auto curveSet : allCurveSetsInPlot )
@ -185,9 +186,9 @@ void RiuSummaryPlot::showContextMenu( QPoint pos )
menuBuilder.appendToMenu( &menu );
if ( menu.actions().size() > 0 )
if ( !menu.actions().empty() )
{
menu.exec( mapToGlobal( pos ) );
menu.exec( plotWidget()->mapToGlobal( pos ) );
// Parts of progress dialog GUI can be present after menu has closed related to
// RicImportGridModelFromSummaryCurveFeature. Make sure the plot is updated, and call processEvents() to make

View File

@ -19,9 +19,9 @@
#pragma once
#include "RiaQDateTimeTools.h"
#include "RiuInterfaceToViewWindow.h"
#include <QWidget>
#include <QObject>
#include <QPoint>
class RimSummaryPlot;
class RimPlotAxisPropertiesInterface;
@ -32,12 +32,12 @@ class RiuPlotWidget;
//
//
//==================================================================================================
class RiuSummaryPlot : public QWidget
class RiuSummaryPlot : public QObject
{
Q_OBJECT
public:
RiuSummaryPlot( RimSummaryPlot* plot, QWidget* parent );
~RiuSummaryPlot() override;
RiuSummaryPlot( RimSummaryPlot* plot );
~RiuSummaryPlot();
virtual void useDateBasedTimeAxis(
const QString& dateFormat,

View File

@ -29,8 +29,8 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuSummaryQtChartsPlot::RiuSummaryQtChartsPlot( RimSummaryPlot* plot, QWidget* parent /*= nullptr*/ )
: RiuSummaryPlot( plot, parent )
RiuSummaryQtChartsPlot::RiuSummaryQtChartsPlot( RimSummaryPlot* plot )
: RiuSummaryPlot( plot )
{
m_plotWidget = new RiuQtChartsPlotWidget( plot );
m_plotWidget->setContextMenuPolicy( Qt::CustomContextMenu );
@ -49,6 +49,8 @@ RiuSummaryQtChartsPlot::RiuSummaryQtChartsPlot( RimSummaryPlot* plot, QWidget* p
//--------------------------------------------------------------------------------------------------
RiuSummaryQtChartsPlot::~RiuSummaryQtChartsPlot()
{
delete m_plotWidget;
m_plotWidget = nullptr;
}
//--------------------------------------------------------------------------------------------------

View File

@ -38,7 +38,7 @@ class RiuSummaryQtChartsPlot : public RiuSummaryPlot
Q_OBJECT;
public:
RiuSummaryQtChartsPlot( RimSummaryPlot* plot, QWidget* parent = nullptr );
RiuSummaryQtChartsPlot( RimSummaryPlot* plot );
~RiuSummaryQtChartsPlot() override;
void useDateBasedTimeAxis( const QString& dateFormat,

View File

@ -94,7 +94,7 @@ static EnsembleCurveInfoTextProvider ensembleCurveInfoTextProvider;
///
//--------------------------------------------------------------------------------------------------
RiuSummaryQwtPlot::RiuSummaryQwtPlot( RimSummaryPlot* plot, QWidget* parent /*= nullptr*/ )
: RiuSummaryPlot( plot, parent )
: RiuSummaryPlot( plot )
{
m_plotWidget = new RiuQwtPlotWidget( plot, parent );
m_plotWidget->setContextMenuPolicy( Qt::CustomContextMenu );
@ -138,6 +138,8 @@ RiuSummaryQwtPlot::RiuSummaryQwtPlot( RimSummaryPlot* plot, QWidget* parent /*=
//--------------------------------------------------------------------------------------------------
RiuSummaryQwtPlot::~RiuSummaryQwtPlot()
{
delete m_plotWidget;
m_plotWidget = nullptr;
}
//--------------------------------------------------------------------------------------------------

View File

@ -45,7 +45,7 @@ class RiuSummaryQwtPlot : public RiuSummaryPlot
public:
RiuSummaryQwtPlot( RimSummaryPlot* plot, QWidget* parent = nullptr );
~RiuSummaryQwtPlot() override;
~RiuSummaryQwtPlot();
void useDateBasedTimeAxis( const QString& dateFormat,
const QString& timeFormat,