Two Questions about Linking to C libraries

Craig Dillabaugh craig.dillabaugh at gmail.com
Tue Nov 26 17:26:41 PST 2013


I recently created bindings to the C libary shapelib.

http://shapelib.maptools.org/

I generated my own bindings by hand, and another set of bindings 
with DStep.

I created a small test program to test my bindings.  My current 
test program in its entirety is:

import std.stdio;
import std.string;
import std.conv;

import shapefil;

int main( string args[] )
{
   const char* filename = std.string.toStringz( args[1] );

   SHPHandle hShp = SHPOpen(filename, "rb");

   int num_entities;
   int shp_type;
   double pad_min_bound;
   double pad_max_bound;

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

   writeln("Shapefile contains " ~ to!string(num_entities) ~ " 
entities.");
   writeln("Of type " ~ to!string( SHPTypeName( shp_type) ));
   writeln("Bounds = [" ~to!string(pad_min_bound) ~ ","
                        ~ to!string(pad_max_bound) ~ "]");

    SHPClose( hShp );
    return 0;
}

It simply reads a Shape File, the name for which is passed from 
the command line.

When I execute this I get the following message:

./shapetest data/dresden/gis.osm_water_v06.shp
Shapefile contains 799 entities.
Of type Polygon
Bounds = [13.5274,50.9632]
Segmentation fault

The first three lines output are what I expected, but then it 
segfaults on exit.
Running it in GDB I get the following error message:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000443fc7 in rt.dmain2._d_run_main() ()

Printing the backtrace provides no additional information.  The 
same problem occurs whether I used my hand-rolled binds or the 
DStep version.

So my two questions are:

1. Does anyone have any idea why my program is segfaulting. It 
seems to crash at exit and I read somewhere (can't recall where) 
that uncollected C style strings may cause the GC to fail.  
Perhaps this is my problem.  I tried commenting out some of the 
statements (eg. the SHPTypeName( shp_type) line, which returns a 
"const char *", but I still get a segfault.  Any ideas on how to 
find the root cause?

2. Once I think my bindings are stable I would like to add them 
to Deimos or DUB registries.  Are there any recommendations for 
testing bindings? I checked through some other bindings on GitHub 
and didn't see any unit tests or the like. Are there any 'best 
practices' out there for testing bindings?




More information about the Digitalmars-d-learn mailing list