Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: SvPV Segmentation Fault

by adler187 (Initiate)
on Apr 25, 2012 at 21:19 UTC ( #967175=note: print w/replies, xml ) Need Help??

in reply to Re: SvPV Segmentation Fault
in thread SvPV Segmentation Fault

Ok, that worked:

./ Starting Build Preprocess Stage + + + Finished Build Preprocess Stage + + + + + + Starting Build Parse Stage Finished Build Parse Stage Starting Build Glue 1 Stage Finished Build Glue 1 Stage Starting Build Glue 2 Stage Finished Build Glue 2 Stage Starting Build Glue 3 Stage Finished Build Glue 3 Stage Starting Build Compile Stage Starting "perl Makefile.PL" Stage Writing Makefile for _967160 Writing MYMETA.yml Finished "perl Makefile.PL" Stage Starting "make" Stage /usr/bin/perl /usr/lib/perl5/5.14.2/ExtUtils/xsubpp -typemap /usr/lib +/perl5/5.14.2/ExtUtils/typemap _967160.xs > _967160.xsc && mv _9671 +60.xsc _967160.c cc -c -I/home/kadler -D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTEN +V -fno-strict-aliasing -pipe -fstack-protector -D_LARGEFILE_SOURCE -D +_FILE_OFFSET_BITS=64 -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 + -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -W +all -pipe -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\" -fPIC "-I/usr/li +b/perl5/5.14.2/x86_64-linux-thread-multi/CORE" _967160.c Running Mkbootstrap for _967160 () chmod 644 rm -f blib/arch/auto/_967160/ cc -shared -L/usr/local/lib64 -fstack-protector _967160.o -o blib/ar +ch/auto/_967160/ \ \ chmod 755 blib/arch/auto/_967160/ cp blib/arch/auto/_967160/ chmod 644 blib/arch/auto/_967160/ Finished "make" Stage Starting "make install" Stage Files found in blib/arch: installing files in blib/lib into architectu +re dependent library tree Installing /home/kadler/_Inline/lib/auto/_967160/ Installing /home/kadler/_Inline/lib/auto/_967160/ Finished "make install" Stage Starting Cleaning Up Stage Finished Cleaning Up Stage Finished Build Compile Stage fred 1

But, I wasn't actually using Inline::C. Just using that as an example of using SvPV and SvPV_nolen to build my XS module. There must be something wrong with building my XS module, then.

I've been using perlxstut as a basis, specifically examples #4 and #5.

Here's mylib.c:
#include <stdlib.h> #include "mylib.h" PerlInterpreter *my_perl; void foo(SV * sv) { if(sv) { printf("%s\n", SvPV_nolen(sv)); } } void bar(SV * sv) { printf ("%i\n", SvPOK(sv)); }
Here's mylib.h:
#include "EXTERN.h" #include "perl.h" #define TESTVAL 4 void foo(SV * sv); void bar(SV * sv);
Here's Mytest2.xs:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "mylib/mylib.h" #include "" MODULE = Mytest2 PACKAGE = Mytest2 INCLUDE: void foo(SV * sv) void bar(SV * sv)
Everything else should be exactly as in perlxstut.

Replies are listed 'Best First'.
Re^3: SvPV Segmentation Fault
by dave_the_m (Prior) on Apr 25, 2012 at 22:21 UTC
    PerlInterpreter *my_perl
    That's very wrong. I don't know why you've put that there. You need to either pass the my_perl arg to the foo/bar functions using aTHX_/pTHX, or (less efficiently), retrieve it locally using dTHX.


      Hmm, was wondering about that. Initially I did not have the declaration, but then I got an error about 'my_perl' undeclared. I found some code online (that I didn't read thoroughly, I see) and declared the variable.

      Apparently, that is not enough. Any good examples/tutorials on how to do this?

      Or perhaps I'm taking the wrong approach here and there is a better way. Here's what I'm trying to do. I'm trying to write a function in C that can be called from perl that takes as input, basically argc and argv. It seems you can't actually pass an array of char * from perl, you need to handle an AV *. I'd like to keep all this code in the external library, instead of writing it in the XS file, but maybe I'm just making it harder for myself that way.

        See perlguts.pod for more details about the [adp]THX_? macros, but basically, you're using a perl compiled for multiplicility/threading, and in those perls, most perl functions (including SvPV()) expect to take an extra first argument, called my_perl, which is a pointer to the current perl interpreter.

        The idea is that on threaded perls, these macros typically expand as follows:

        aTHX my_perl aTHX_ my_perl, pTHX PerlInterpreter* my_perl pTHX_ PerlInterpreter* my_perl, dTHX PerlInterpreter* my_perl = something_that_retrieves_the_current_perl();
        while on unthreaded perls they expand to null strings.

        You typically declare any function as expecting an interpreter as the first arg:

        void foo(pTHX_ SV * sv) { ... }
        and invoke it as
        foo(aTHX_ some_sv);

        Normally your XS sub will have been passed a my_perl arg, and you can pass this on down the chain of called subs using aTHX/pTHX. In those cases where this isn't possible, dTHX can be used within the declaration section of a function to recreate my_perl 'on the fly', but it's expensive:

        void foo(SV * sv) { dTHX; ... }

        All the above assumes that PERL_NO_GET_CONTEXT is defined in your source. If it isn't then aTHX is redefined to directly compute the interpreter address, like dTHX. This means everything 'just works' without messing with aTHX/pTHX, but is slow. Since you were getting compilation errors about missing my_perl, I'm assuming you must have had PERL_NO_GET_CONTEXT defined.


Re^3: SvPV Segmentation Fault
by BrowserUk (Pope) on Apr 25, 2012 at 21:47 UTC

    I'd start by looking for differences in the generated C files between the Inline::C and XS versions. Also, a comparison of the respective's might shed some light.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967175]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2017-04-29 13:22 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.