Hi again,
In fact I've noticed that even if I don't use AUTOWRAP it still
crashes. I've inlined a small C function in the pel script which simply
delegates to the function in the library:
use Inline C => <<'END_OF_C_CODE';
void dumpHash2(SV* hash_ref)
{
dumpHash(hash_ref); // this is the function in the C library
}
END_OF_C_CODE
And it still segfaults in the library, the same as as earlier.
The C code is copied verbatim from the Cookbook. What's different when
running inside the library? Maybe some mysterious changes that Inline::C
does to the inlined C code ?
Please help, thanks!
lr
laurentiu r wrote:
Hi all,
I've encountered some problems and I'm wondering if it's ok to do what
I'm trying to do: can I use Inline::C with the AUTOWRAP option to call
a C library whose functions take variables of Perl types? (the same
way it would use them if the code were inlined in the perl script)
I mean SV*, HV* etc - the ones in libperl and declared in perl.h,
EXTERN.h etc.
I've had no errors with compiling and building, at run time I can even
call some functions that use scalar types (SV*) to receive an "int" as
argument but if I'm passing a hash reference a segfault happens when
trying to iterate through the keys in the hash.
My idea was to be able to pass hashes and other complex types from
perl to C. So I have a test function in the C library which takes a
hash reference as a SV*, then converts that to a HV* and starts
iterating through it (it's taken from the examples in the C CookBook):
-----------------
void dumpHash(SV* hash_ref)
{
HV* hash;
HE* hash_entry;
int num_keys, i;
SV* sv_key;
SV* sv_val;
if (! SvROK(hash_ref))
croak("hash_ref is not a reference");
hash = (HV*)SvRV(hash_ref);
num_keys = hv_iterinit(hash); // <<<<<<<---- segfaults here
for (i = 0; i < num_keys; i++) {
hash_entry = hv_iternext(hash);
sv_key = hv_iterkeysv(hash_entry);
sv_val = hv_iterval(hash, hash_entry);
printf("%s => %s\n", SvPV(sv_key, PL_na), SvPV(sv_val,
PL_na));
}
return;
}
-----------------
Then I call the function from a perl script which uses Inline like that:
(the C library "libcperlbindings.a" is built under cstuff/ directory)
-----------------
#!/usr/bin/perl
package CBindings;
use Inline C => Config =>
ENABLE => AUTOWRAP =>
LIBS => '-L/root/aaa/perl_inline/cstuff/.libs/ -lcperlbindings',
INC => '-I/root/aaa/perl_inline/cstuff/';
use Inline C => q{ void dumpHash(SV*); };
package main;
my %hash = (
Author => "Brian Ingerson",
Nickname => "INGY",
Module => "Inline.pm",
Version => "0.30",
Language => "C",
);
CBindings::dumpHash(\$hash); # <<<<<<----- segfaults here
--------------------
When running this it gets a segfault and using a debugger I've noticed
that the call that crashes is the one on the line with "num_keys =
hv_iterinit(hash);" (marked in the source code too)
Can anyone see the problem? Isn't this supposed to work with AUTOWRAP ?
Or am I doing it the wrong way?
It can iterate just fine if I inline the C function in the Perl script
(without AUTOWRAP).
Any help is greately apreciated, thanks!
Laurentiu