Help Finding Strange Memory Bug (Code linked to C library)

Craig Dillabaugh craig.dillabaugh at gmail.com
Sat Dec 7 07:29:51 PST 2013


Hello,
I recently wrote bindings to the C-library Shapelib (it 
reads/writes a common file format used in Geographic Information 
Systems).

I've been trying to write a small test program to make sure my 
bindings 'work' and I've come across a bizarre memory bug.  I  
THINK I've identified the code that causes the problem, but I 
have no idea why.

My test-suite includes this function:

void shapeRead(string filename) {
   SHPHandle hShp = SHPOpen( std.string.toStringz( filename ), 
"rb" );

   int n, shp_type;
   double pad_min_bound, pad_max_bound;

   SHPGetInfo( hShp, &n, &shp_type, &pad_min_bound, 
&pad_max_bound);

   SHPClose( hShp );
}

If I comment out the SHPGetInfo call, then the segmentation fault 
doesn't happen, but if its there then the program segfaults AFTER 
the shapeRead function exits (eg. SHPClose runs fine) ).

In fact if I call SHPGetInfo as follows, the crash doesn't occur:
SHPGetInfo( hShp, &n, &shp_type, &pad_min_bound, null); //NULL 
pointer last arg.

So in C the function SHPGetInfo is:

void SHPAPI_CALL
       SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * 
pnShapeType,
                   double * padfMinBound, double * padfMaxBound );

While my D binding is (pretty much the same):

extern( C ) void SHPGetInfo( SHPHandle hSHP, int* pnEntities,
	  int* pnShapeType, double* padfMinBound, double* padfMaxBound );


I have no idea what is going on.  The sizes of ints and doubles 
are 4 and 8 bytes using gcc on my system (which is how I compiled 
my C library) so those match the sizes of the corresponding D 
types [I thought maybe there was a type-size mismatch and that 
was causing something to be overwritten, but it doesn't appear 
that way].

Any hints on where to look next would be appreciated.

Craig





More information about the Digitalmars-d-learn mailing list