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:
It dies horribly withsay "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;
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 | |
by pokki (Monk) on Feb 13, 2017 at 09:41 UTC | |
by Corion (Patriarch) on Feb 13, 2017 at 09:51 UTC | |
by pokki (Monk) on Feb 14, 2017 at 10:13 UTC |