Skip to content

Commit 4888f51

Browse files
committed
[backport] Fix bug where histogram can be assigned negative frequency for a pixel range. Also fix potential memory leak as new histogram vector was assigned to band stats without clearing the old.
1 parent b2b6c5a commit 4888f51

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/core/raster/qgsrasterlayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider,
24402440
myRasterBandStats.bandName = mDataProvider->generateBandName( i );
24412441
myRasterBandStats.bandNumber = i;
24422442
myRasterBandStats.statsGathered = false;
2443-
myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector();
2443+
myRasterBandStats.histogramVector->clear();
24442444
//Store the default color table
24452445
//readColorTable( i, &myRasterBandStats.colorTable );
24462446
QList<QgsColorRampShader::ColorRampItem> ct;

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,16 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & t
13271327

13281328
for ( int myBin = 0; myBin < theBinCount; myBin++ )
13291329
{
1330-
theBandStats.histogramVector->push_back( myHistogramArray[myBin] );
1331-
QgsDebugMsg( "Added " + QString::number( myHistogramArray[myBin] ) + " to histogram vector" );
1330+
if ( myHistogramArray[myBin] < 0 ) //can't have less than 0 pixels of any value
1331+
{
1332+
theBandStats.histogramVector->push_back( 0 );
1333+
QgsDebugMsg( "Added 0 to histogram vector as freq was negative!" );
1334+
}
1335+
else
1336+
{
1337+
theBandStats.histogramVector->push_back( myHistogramArray[myBin] );
1338+
QgsDebugMsg( "Added " + QString::number( myHistogramArray[myBin] ) + " to histogram vector" );
1339+
}
13321340
}
13331341

13341342
}

0 commit comments

Comments
 (0)