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.


In reply to Wrapping C constructor with opaque pointer using Platypus by pokki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.