Skip to content

Commit 9162fc4

Browse files
author
ersts
committed
-Fixed problem when loading of default style, closes ticket #1480
-Added additional initialization of QgsRasterLayer class variables -Plugged a memory leak when chaning shaders git-svn-id: http://svn.osgeo.org/qgis/trunk@10172 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ac0b428 commit 9162fc4

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

src/core/raster/qgsrasterlayer.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,31 @@ QgsRasterLayer::QgsRasterLayer(
8585
mInvertColor( false )
8686
{
8787

88+
mRasterType = QgsRasterLayer::GrayOrUndefined;
89+
90+
mRedBandName = TRSTRING_NOT_SET;
91+
mGreenBandName = TRSTRING_NOT_SET;
92+
mBlueBandName = TRSTRING_NOT_SET;
93+
mGrayBandName = TRSTRING_NOT_SET;
94+
mTransparencyBandName = TRSTRING_NOT_SET;
95+
96+
8897
mUserDefinedRGBMinimumMaximum = false; //defaults needed to bypass enhanceContrast
8998
mUserDefinedGrayMinimumMaximum = false;
9099
mRGBMinimumMaximumEstimated = true;
91100
mGrayMinimumMaximumEstimated = true;
92101

102+
mDrawingStyle = QgsRasterLayer::UndefinedDrawingStyle;
103+
mContrastEnhancementAlgorithm = QgsContrastEnhancement::NoEnhancement;
104+
mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
93105
mRasterShader = new QgsRasterShader();
94106

95-
if ( loadDefaultStyleFlag )
96-
{
97-
bool defaultLoadedFlag = false;
98-
loadDefaultStyle( defaultLoadedFlag );
99-
if ( defaultLoadedFlag )
100-
{
101-
return;
102-
}
103-
}
107+
mHasPyramids = false;
108+
mNoDataValue = -9999;
109+
mValidNoDataValue = false;
110+
111+
mGdalBaseDataset = 0;
112+
mGdalDataset = 0;
104113

105114
// Initialise the affine transform matrix
106115
mGeoTransform[0] = 0;
@@ -121,6 +130,18 @@ QgsRasterLayer::QgsRasterLayer(
121130
if ( ! path.isEmpty() )
122131
{
123132
readFile( path ); // XXX check for failure?
133+
134+
//readFile() is really an extension of the constructor as many imporant fields are set in this method
135+
//loadDefaultStyle() can not be called before the layer has actually be opened
136+
if ( loadDefaultStyleFlag )
137+
{
138+
bool defaultLoadedFlag = false;
139+
loadDefaultStyle( defaultLoadedFlag );
140+
if ( defaultLoadedFlag )
141+
{
142+
return;
143+
}
144+
}
124145
}
125146

126147
} // QgsRasterLayer ctor
@@ -199,6 +220,10 @@ QgsRasterLayer::~QgsRasterLayer()
199220
if ( mGdalBaseDataset )
200221
{
201222
GDALDereferenceDataset( mGdalBaseDataset );
223+
}
224+
225+
if( mGdalDataset )
226+
{
202227
GDALClose( mGdalDataset );
203228
}
204229
}
@@ -3162,8 +3187,6 @@ void QgsRasterLayer::setColorShadingAlgorithm( ColorShadingAlgorithm theShadingA
31623187
mRasterShader = new QgsRasterShader();
31633188
}
31643189

3165-
mColorShadingAlgorithm = theShadingAlgorithm;
3166-
31673190
switch ( theShadingAlgorithm )
31683191
{
31693192
case PseudoColorShader:
@@ -3182,6 +3205,9 @@ void QgsRasterLayer::setColorShadingAlgorithm( ColorShadingAlgorithm theShadingA
31823205
mRasterShader->setRasterShaderFunction( new QgsRasterShaderFunction() );
31833206
break;
31843207
}
3208+
3209+
//Set the class variable after the call to setRasterShader(), so memory recovery can happen
3210+
mColorShadingAlgorithm = theShadingAlgorithm;
31853211
}
31863212
QgsDebugMsg( "mColorShadingAlgorithm = " + QString::number( theShadingAlgorithm ) );
31873213
}
@@ -3378,6 +3404,12 @@ void QgsRasterLayer::setNoDataValue( double theNoDataValue )
33783404

33793405
void QgsRasterLayer::setRasterShaderFunction( QgsRasterShaderFunction* theFunction )
33803406
{
3407+
//Free old shader if it is not a userdefined shader
3408+
if( mColorShadingAlgorithm != QgsRasterLayer::UserDefinedShader && 0 != mRasterShader->rasterShaderFunction() )
3409+
{
3410+
delete( mRasterShader->rasterShaderFunction() );
3411+
}
3412+
33813413
if ( theFunction )
33823414
{
33833415
mRasterShader->setRasterShaderFunction( theFunction );
@@ -3387,7 +3419,7 @@ void QgsRasterLayer::setRasterShaderFunction( QgsRasterShaderFunction* theFuncti
33873419
{
33883420
//If NULL as passed in, set a default shader function to prevent segfaults
33893421
mRasterShader->setRasterShaderFunction( new QgsRasterShaderFunction() );
3390-
mColorShadingAlgorithm = QgsRasterLayer::UserDefinedShader;
3422+
mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
33913423
}
33923424
}
33933425

@@ -5175,6 +5207,8 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
51755207
//Set up a new color ramp shader
51765208
setColorShadingAlgorithm( ColorRampShader );
51775209
QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader->rasterShaderFunction();
5210+
//TODO: Make sure the set algorithm and cast was successful,
5211+
//e.g., if ( 0 != myColorRampShader && myColorRampShader->shaderTypeAsString == "ColorRampShader" )
51785212
myColorRampShader->setColorRampType( QgsColorRampShader::INTERPOLATED );
51795213
myColorRampShader->setColorRampItemList( *colorTable( 1 ) );
51805214
}

0 commit comments

Comments
 (0)