Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^4: SvPV Segmentation Fault

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

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

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.

Replies are listed 'Best First'.
Re^5: SvPV Segmentation Fault
by dave_the_m (Prior) on Apr 25, 2012 at 23:14 UTC
    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.


      Thanks, I also found this page when looking up dTHX on google. Looks like I should be able to figure it out now.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967197]
[james28909]: Good morning!
[james28909]: I have a problem I am trying to solve. I am packaging scripts using pdk and would like the verify the executable name each time it is executed
[Corion]: Hi James!
[james28909]: I looked through the docs, and see that PerlApp::exe() is suppose to return this information but whenever i try to use this, i get the error "Undefined subroutine &PerlApp::exe called"
[james28909]: i just want to verify the integrity of the executables name each time it is run.
[Corion]: Weird. this documentation says what you say.
[Corion]: I assume you are experiencing this with your packaged program. Maybe also include in the packaging list?
[james28909]: has anyone else run into this problem? i have tried searching but have not returned many results
[james28909]: ah, good idea.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2017-07-28 13:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (429 votes). Check out past polls.