pokki has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm not sure of the terminology because I am crap at C, so hopefully you'll understand what I mean from the code (and I can edit the question).

I am trying to wrap a C library called igraph. I've managed to build and install it and even written an Alien::igraph wrapper. Now I want to use Platypus to provide a Perl interface for it. The end goal is to provide a replacement for Graph... but right now I just want to get the basics working.

My code so far, running on Perl 5.18.4:

package Graph::igraph; use strict; use warnings; use 5.012; use Carp; use autodie; use utf8; use Alien::igraph; use FFI::Platypus; use FFI::Platypus::API; my $ffi = FFI::Platypus->new; $ffi->lib(Alien::igraph->dynamic_libs); $ffi->type('int' => 'igraph_integer_t'); $ffi->type('int' => 'igraph_bool_t'); $ffi->type('double' => 'igraph_real_t'); $ffi->type('opaque' => 'igraph_t'); $ffi->load_custom_type('::StringPointer' => 'string_pointer'); $ffi->attach([igraph_empty => 'new'] => [qw/igraph_t igraph_integer_t +igraph_bool_t/] => 'int', sub { my ($xsub, $class, %options) = @_; my $graph; if ($options{undirected}) { say 'undirected -- ' . $xsub->(\$graph, 0, 0); } else { say 'directed -- ' . $xsub->(\$graph, 0, 1); } say 'weird: this line says "Use of uninitialized valu +e in say"'; return bless \$graph, $class; }); $ffi->attach([igraph_destroy => 'DESTROY'] => [qw/igraph_t/] => 'int') +; $ffi->attach(igraph_vcount => ['igraph_t'] => 'igraph_integer_t'); $ffi->attach(igraph_add_vertices => [qw/igraph_t igraph_integer_t/] => + 'int'); $ffi->attach(igraph_version => [qw/string_pointer int* int* int*/] => +'int'); sub version { my ($version, $major, $minor, $patch); igraph_version(\$version, \$major, \$minor, \$patch); return ($version, $major, $minor, $patch); } sub add_vertex { my $self = shift; igraph_add_vertices($self, 1); } sub count_vertices { my $self = shift; igraph_vcount($self); } 1;

So I can tell *something* went right, because this works:

say "version: " . join(' ', Graph::igraph::version()); # prints 0.7.1 +0 7 1

But when I run the following:

say "version: " . join(' ', Graph::igraph::version()); my $graph = Graph::igraph->new(directed => 1); use Data::Dumper; print Dumper($graph); $graph->add_vertex('foobar'); say $graph->count_vertices;
It dies horribly with
version: 0.7.1 0 7 1 directed -- 0 Use of uninitialized value in say at lib/Graph/igraph.pm line 34. Attempt to free unreferenced scalar: SV 0x21213d8 at /home/fgabolde/pe +rl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/x86_64-linux/FFI/ +Platypus.pm line 331. $VAR1 = bless( do{\(my $o = undef)}, 'Graph::igraph' ); *** Error in `perl': realloc(): invalid next size: 0x000000000223f850 +***

and then a backtrace, a memory map, and finally Aborted (core dumped). Note the weird say statement in the new wrapper. Note also that the igraph_empty constructor (documented here) returns 0, so the graph initialization is supposed to have succeeded.

The Platypus docs do have some examples with opaque pointers where the pointers are returned by C "constructors"; unfortunately the igraph API requires that one pass an igraph_t* that will get initialized, see the doc above.

I've done some XS work before but nothing much, so I am mostly lost at sea here as you can probably tell.

Replies are listed 'Best First'.
Re: Wrapping C constructor with opaque pointer using Platypus
by Anonymous Monk on Feb 10, 2017 at 17:44 UTC

    Hello there. Unfortunately I can't find the Alien::igraph, did you post that somewhere? No matter though.

    From a cursory look at the igraph library, it seems igraph API expects you to manage the allocation of igraph_t structure on your own. Either a static or malloc'ed struct, but you have to allocate it somewhere. From this I presume you'd want a chunk (sizeof(igraph_t)) of opaque data in your perl object, not an opaque pointer. HtH.

      Sorry for lateness, I was away on week-end.

      Hello there. Unfortunately I can't find the Alien::igraph, did you post that somewhere? No matter though.

      It's on a bitbucket repository only right now: https://bitbucket.org/fgabolde/alien-igraph

      From a cursory look at the igraph library, it seems igraph API expects you to manage the allocation of igraph_t structure on your own. Either a static or malloc'ed struct, but you have to allocate it somewhere. From this I presume you'd want a chunk (sizeof(igraph_t)) of opaque data in your perl object, not an opaque pointer. HtH.

      Like I said, I don't know much about C, but it does not look like the library expects me to allocate memory. From the API docs:

      igraph_t g; igraph_empty(&g, 0, 1);

      seems to be enough to get an initialized graph in g. If this were XS I'd just do that but I don't really understand how to drop down to C in Platypus.

        This allocates memory, either on the stack or in the area for global memory:

        igraph_t g;

        For Perl, you will need to allocate a buffer, more or less with:

        use vars '$g'; $g = "\x00" x IGRAPH_T_SIZE;

        ... and ideally you can call/compile sizeof(igraph_t) in C so your C compiler and Perl agree on how much memory should be allocated for it.