www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Two Questions about Linking to C libraries

reply "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
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?
Nov 26 2013
next sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
Don't have answers. Do you still get segfault removing SHPClose( 
hShp );

Other comment:


   writeln("Bounds = [" ~to!string(pad_min_bound) ~ ","
                        ~ to!string(pad_max_bound) ~ "]");
writeln("Bounds = [", pad_min_bound, ",", pad_max_bound, "]");
Nov 26 2013
parent "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 27 November 2013 at 02:36:01 UTC, Jesse Phillips 
wrote:
 Don't have answers. Do you still get segfault removing 
 SHPClose( hShp );
Yep.
 Other comment:


  writeln("Bounds = [" ~to!string(pad_min_bound) ~ ","
                       ~ to!string(pad_max_bound) ~ "]");
writeln("Bounds = [", pad_min_bound, ",", pad_max_bound, "]");
Thanks, thats look a bit nicer.
Nov 26 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-11-27 02:26, Craig Dillabaugh wrote:

 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?
I don't think there's an easy way to do unit testing for bindings. What you could do is port the original unit tests if available. Or create some new unit tests if none exists. -- /Jacob Carlborg
Nov 26 2013
parent "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 27 November 2013 at 07:30:58 UTC, Jacob Carlborg 
wrote:
 On 2013-11-27 02:26, Craig Dillabaugh wrote:

 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?
I don't think there's an easy way to do unit testing for bindings. What you could do is port the original unit tests if available. Or create some new unit tests if none exists.
Thanks. There were no unit tests with the original code. I will take a shot at it.
Nov 27 2013