Skip to content

Commit

Permalink
fix grass crash:
Browse files Browse the repository at this point in the history
  setlocale to "C" in getSRS() and catch fatal errors
  (probably needed at much more places).


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8476 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed May 20, 2008
1 parent 0c9b20e commit 77dfd46
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/plugins/grass/qgsgrassselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ QStringList QgsGrassSelect::vectorLayers ( QString gisdbase,
level = Vect_open_old_head (&map, (char *) mapName.ascii(),
(char *) mapset.ascii());
}
QgsGrass::clearErrorEnv();

if ( QgsGrass::getError() == QgsGrass::FATAL ) {
std::cerr << "Cannot open GRASS vector: " << QgsGrass::getErrorMessage().toLocal8Bit().data() << std::endl;
Expand Down
23 changes: 20 additions & 3 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/* $Id$ */

#include <iostream>
#include <qgslogger.h>

#include "QString"
#include "q3process.h"
Expand Down Expand Up @@ -375,6 +376,7 @@ QString QgsGrass::mGisrc;
QString QgsGrass::mTmp;

jmp_buf QgsGrass::mFatalErrorEnv;
bool QgsGrass::mFatalErrorEnvActive=false;

int QgsGrass::error_routine ( char *msg, int fatal)
{
Expand All @@ -383,15 +385,21 @@ int QgsGrass::error_routine ( char *msg, int fatal)

int QgsGrass::error_routine ( const char *msg, int fatal)
{
std::cerr << "error_routine (fatal = " << fatal << "): " << msg << std::endl;
QgsDebugMsg(QString("error_routine (fatal = %1): %2").arg(fatal).arg(msg));

error_message = msg;

if ( fatal )
{
{
error = FATAL;
// we have to do a long jump here, otherwise GRASS >= 6.3 will kill our process
longjmp(mFatalErrorEnv, 1);
if( mFatalErrorEnvActive )
longjmp(mFatalErrorEnv, 1);
else
{
QMessageBox::warning( 0, QObject::tr("Uncatched fatal GRASS error"), msg );
abort();
}
}
else
error = WARNING;
Expand All @@ -416,9 +424,18 @@ QString GRASS_EXPORT QgsGrass::getErrorMessage ( void )

jmp_buf GRASS_EXPORT &QgsGrass::fatalErrorEnv()
{
if(mFatalErrorEnvActive)
QgsDebugMsg("fatal error environment already active.");
mFatalErrorEnvActive = true;
return mFatalErrorEnv;
}

void GRASS_EXPORT QgsGrass::clearErrorEnv()
{
if(!mFatalErrorEnvActive)
QgsDebugMsg("fatal error environment already deactive.");
mFatalErrorEnvActive = false;
}

QString GRASS_EXPORT QgsGrass::openMapset ( QString gisdbase, QString location, QString mapset )
{
Expand Down
2 changes: 2 additions & 0 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class QgsGrass {
static GRASS_EXPORT QString versionString();

static GRASS_EXPORT jmp_buf& fatalErrorEnv();
static GRASS_EXPORT void clearErrorEnv();


private:
Expand Down Expand Up @@ -184,6 +185,7 @@ class QgsGrass {

// Context saved before a call to routine that can produce a fatal error
static jmp_buf mFatalErrorEnv;
static bool mFatalErrorEnvActive;
};

#endif // QGSGRASS_H
20 changes: 19 additions & 1 deletion src/providers/grass/qgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,24 @@ QgsSpatialRefSys QgsGrassProvider::getSRS()

struct Cell_head cellhd;

QgsGrass::resetError();
QgsGrass::setLocation ( mGisdbase, mLocation );
G_get_default_window(&cellhd);

char *oldlocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "C");

if ( setjmp(QgsGrass::fatalErrorEnv()) == 0 )
{
G_get_default_window(&cellhd);
}
QgsGrass::clearErrorEnv();

if ( QgsGrass::getError() == QgsGrass::FATAL ) {
setlocale(LC_ALL, oldlocale);
QgsDebugMsg(QString("Cannot get default window: %1").arg(QgsGrass::getErrorMessage()));
return QgsSpatialRefSys();
}

if (cellhd.proj != PROJECTION_XY) {
struct Key_Value *projinfo = G_get_projinfo();
struct Key_Value *projunits = G_get_projunits();
Expand All @@ -1260,6 +1276,8 @@ QgsSpatialRefSys QgsGrassProvider::getSRS()
free ( wkt);
}

setlocale(LC_ALL, oldlocale);

QgsSpatialRefSys srs;
srs.createFromWkt(WKT);

Expand Down

0 comments on commit 77dfd46

Please sign in to comment.