Skip to content

Commit 4c84cfa

Browse files
wonder-sknyalldawson
authored andcommittedJun 21, 2018
Fix a memory leak in classify() + add missing annotations/docstrings
A pseudo color renderer was getting created in classify() but never deleted. Spotted by @PeterPetrik As a bonus switched a for loop from mixed constBegin()/end() usage to range for.
1 parent 506f028 commit 4c84cfa

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed
 

‎python/core/auto_generated/raster/qgscolorrampshader.sip.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ A ramp shader will color a raster pixel based on a list of values ranges in a ra
3535
Quantile
3636
};
3737

38-
QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp = 0, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
38+
QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp /Transfer/ = 0, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
3939
%Docstring
4040
Creates a new color ramp shader.
4141

4242
:param minimumValue: minimum value for the raster shader
4343
:param maximumValue: maximum value for the raster shader
4444
:param type: interpolation type used
4545
:param classificationMode: method used to classify the color ramp shader
46-
:param colorRamp: vector color ramp used to classify the color ramp shader
46+
:param colorRamp: vector color ramp used to classify the color ramp shader. Ownership is transferred to the shader.
4747

4848
:return: new QgsColorRampShader
4949
%End
@@ -105,7 +105,7 @@ Gets the source color ramp
105105

106106
void setSourceColorRamp( QgsColorRamp *colorramp /Transfer/ );
107107
%Docstring
108-
Set the source color ramp. Ownership is transferred to the renderer.
108+
Set the source color ramp. Ownership is transferred to the shader.
109109

110110
.. seealso:: :py:func:`sourceColorRamp`
111111

‎python/core/auto_generated/raster/qgssinglebandpseudocolorrenderer.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Returns the raster shader
5656
available in Python as constShader
5757
%End
5858

59-
void createShader( QgsColorRamp *colorRamp = 0,
59+
void createShader( QgsColorRamp *colorRamp /Transfer/ = 0,
6060
QgsColorRampShader::Type colorRampType = QgsColorRampShader::Interpolated,
6161
QgsColorRampShader::ClassificationMode classificationMode = QgsColorRampShader::Continuous,
6262
int classes = 0,
@@ -65,7 +65,7 @@ Returns the raster shader
6565
%Docstring
6666
Creates a color ramp shader
6767

68-
:param colorRamp: vector color ramp
68+
:param colorRamp: vector color ramp. Ownership is transferred to the shader.
6969
:param colorRampType: type of color ramp shader
7070
:param classificationMode: classification mode
7171
:param classes: number of classes

‎src/core/raster/qgscolorrampshader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
6363
* \param maximumValue maximum value for the raster shader
6464
* \param type interpolation type used
6565
* \param classificationMode method used to classify the color ramp shader
66-
* \param colorRamp vector color ramp used to classify the color ramp shader
66+
* \param colorRamp vector color ramp used to classify the color ramp shader. Ownership is transferred to the shader.
6767
* \returns new QgsColorRampShader
6868
*/
69-
QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp = nullptr, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
69+
QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp SIP_TRANSFER = nullptr, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
7070

7171
/**
7272
* Copy constructor
@@ -124,7 +124,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
124124
QgsColorRamp *sourceColorRamp() const SIP_FACTORY;
125125

126126
/**
127-
* Set the source color ramp. Ownership is transferred to the renderer.
127+
* Set the source color ramp. Ownership is transferred to the shader.
128128
* \see sourceColorRamp()
129129
* \since QGIS 3.0
130130
*/

‎src/core/raster/qgssinglebandpseudocolorrenderer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ class CORE_EXPORT QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
6262

6363
/**
6464
* Creates a color ramp shader
65-
* \param colorRamp vector color ramp
65+
* \param colorRamp vector color ramp. Ownership is transferred to the shader.
6666
* \param colorRampType type of color ramp shader
6767
* \param classificationMode classification mode
6868
* \param classes number of classes
6969
* \param clip clip out of range values
7070
* \param extent extent used in classification (only used in quantile mode)
7171
*/
72-
void createShader( QgsColorRamp *colorRamp = nullptr,
72+
void createShader( QgsColorRamp *colorRamp SIP_TRANSFER = nullptr,
7373
QgsColorRampShader::Type colorRampType = QgsColorRampShader::Interpolated,
7474
QgsColorRampShader::ClassificationMode classificationMode = QgsColorRampShader::Continuous,
7575
int classes = 0,

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ void QgsSingleBandPseudoColorRendererWidget::classify()
348348
return;
349349
}
350350

351-
QgsSingleBandPseudoColorRenderer *pr = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), mBandComboBox->currentBand(), nullptr );
351+
std::unique_ptr<QgsSingleBandPseudoColorRenderer> pr( new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), mBandComboBox->currentBand(), nullptr ) );
352352
pr->setClassificationMin( lineEditValue( mMinLineEdit ) );
353353
pr->setClassificationMax( lineEditValue( mMaxLineEdit ) );
354-
pr->createShader( ramp.get(), static_cast< QgsColorRampShader::Type >( mColorInterpolationComboBox->currentData().toInt() ), static_cast< QgsColorRampShader::ClassificationMode >( mClassificationModeComboBox->currentData().toInt() ), mNumberOfEntriesSpinBox->value(), mClipCheckBox->isChecked(), minMaxWidget()->extent() );
354+
pr->createShader( ramp.release(), static_cast< QgsColorRampShader::Type >( mColorInterpolationComboBox->currentData().toInt() ), static_cast< QgsColorRampShader::ClassificationMode >( mClassificationModeComboBox->currentData().toInt() ), mNumberOfEntriesSpinBox->value(), mClipCheckBox->isChecked(), minMaxWidget()->extent() );
355355

356356
const QgsRasterShader *rasterShader = pr->shader();
357357
if ( rasterShader )
@@ -362,13 +362,12 @@ void QgsSingleBandPseudoColorRendererWidget::classify()
362362
mColormapTreeWidget->clear();
363363

364364
const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
365-
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
366-
for ( ; it != colorRampItemList.end(); ++it )
365+
for ( const QgsColorRampShader::ColorRampItem &item : colorRampItemList )
367366
{
368367
QgsTreeWidgetItemObject *newItem = new QgsTreeWidgetItemObject( mColormapTreeWidget );
369-
newItem->setText( ValueColumn, QString::number( it->value, 'g', 15 ) );
370-
newItem->setBackground( ColorColumn, QBrush( it->color ) );
371-
newItem->setText( LabelColumn, it->label );
368+
newItem->setText( ValueColumn, QString::number( item.value, 'g', 15 ) );
369+
newItem->setBackground( ColorColumn, QBrush( item.color ) );
370+
newItem->setText( LabelColumn, item.label );
372371
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
373372
connect( newItem, &QgsTreeWidgetItemObject::itemEdited,
374373
this, &QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited );

0 commit comments

Comments
 (0)
Please sign in to comment.