[Gdal-dev] Re: A list of things in the wrappers and geom type in ogr

Ari Jolma ari.jolma at tkk.fi
Mon Oct 10 14:53:04 EDT 2005


Charlie Savage kirjoitti:

> The OGR c api returns error codes instead of throwing exceptions.   


ogr.i throws:

 OGRErr SetGeometry(OGRGeometryShadow* geom) {
    OGRErr err = OGR_F_SetGeometry(self, geom);
    if (err != 0)
      throw err;
    return 0;
  }

and because there is no catch, this causes an abort and

> So there isn't a need for catch.  Thus the generated code should look 
> something like this:
>
>    CPLErrorReset();
>    result = (OGRErr)OGRFeatureShadow_SetGeometry(arg1,arg2);
>
>    CPLErr eclass = CPLGetLastErrorType();
>    if ( eclass == CE_Failure || eclass == CE_Fatal ) {
>       SWIG_exception( SWIG_RuntimeError, CPLGetLastErrorMsg() );
>    }


it never gets to the swig_exception

>
>
>> Still one more thing, null pointers and wrapper functions. Is the 
>> plan to test for inappropriate null pointers in the wrapper 
>> functions, or do we write the checks into typemaps? Now the situation 
>> is still that it is far too easy to create a segfault with undefined 
>> script variables thrown at methods.
>
>
> What do you mean by a null pointer?  Do you mean passing a null object 
> in the target language to OGR?  Do you have an example you could share?


For some reason (well, there is a spelled out reason at somewhere in 
swig docs) swig makes a null our of undef in Perl (and lazy Perl 
programmers easily have undefs) and passes that to the underlying 
library, which is ok sometimes but not always, in fact usually not and 
they easily cause a segfault.

For example SetProjection in Dataset.i, it takes (char *prj) parameter. 
If I give it uninitialized (undef) $prj in Perl, I get a segfault. There 
are two possibilities: 1) I write

%typemap(perl5,in) (char const *prj)
{
  /* %typemap(freearg) (char const *prj) */
  if ( !$1 ) {
    croak("null argument is not allowed");
    SWIG_fail;
  }
}

or we write in Dataset.i

 CPLErr SetProjection( char const *prj ) {
    if (!prj) return SOME_ERROR_CODE;
    return GDALSetProjection( self, prj );
  }

I discussed this with Kevin a while back but I think I was forgotten at 
some point.

Ah, one more thing to this list: in osr.i there is

~OSRSpatialReferenceShadow() {
    if (OSRDereference( self ) == 0 ) {
      OSRDestroySpatialReference( self );
    }
  }

It seems to me that OSRDereference is currently stopping the 
OSRSpatialReferenceShadow being properly disposed of (I saw this in 
debugging, there are also some (Kevin's?) comments near this in osr.i). 
At least datasource.createlayer makes a copy of the osr, I'm not sure of 
other methods.

Ari

>
> Thanks,
>
> Charlie
>


-- 
Prof. Ari Jolma
Kartografia ja Geoinformatiikka / Cartography and Geoinformatics
Teknillinen Korkeakoulu / Helsinki University of Technology
POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma




More information about the Gdal-dev mailing list