Seg Fault

Discussion in 'Perl Misc' started by satya manapragada, Oct 26, 2005.

  1. Hi all,

    I am fairly new to embedding perl into c. I am trying to rewrite an
    application. which has following code. This code used to work fine with
    perl 5.0005 on BeOS, but when started to compile against perl 5.8.4 on
    Linux, initially it complained unsatisfied variable my_perl variable so
    i added "#include <XSUB.h> " header which solved it. But know when I
    run it i get Segmentation fault.

    here is code

    #include <EXTERN.h>
    #include <perl.h>
    #include <XSUB.h>

    ..........
    ..........
    ..........
    GetBool(
    char *inChan,
    bool *outVal,
    int16 inIdx,
    unsigned int *inRef)
    {
    unsigned int retVal = 0;
    unsigned int tempRef = 0;
    int count;
    char kindStr[8];

    dSP ;

    printf("Hello World \n");
    ENTER ;
    SAVETMPS ;

    PUSHMARK(SP) ;
    XPUSHs(sv_2mortal(newSViv(eCallType_GetBool)));
    XPUSHs(sv_2mortal(newSViv(inChan)));
    XPUSHs(sv_2mortal(newSVpv(kindStr, 0)));
    XPUSHs(sv_2mortal(newSViv(inIdx)));
    XPUSHs(sv_2mortal(newSViv(*inRef)));
    PUTBACK ;

    count = perl_call_pv((char *)Name(), G_ARRAY | G_EVAL);
    SPAGAIN ;

    if (count == 3) { // if we don't return 3, then we disregard whatever
    this function did.
    // return bool, ref, and callback (reversed om stack)
    retVal = POPl;
    *inRef = POPl;
    *outVal = POPl ? true : false;
    }

    PUTBACK ;
    FREETMPS ;
    LEAVE ;

    return retVal;
    }
    .............
    .............
    .............
    (gdb) continue
    Continuing.

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1232290896 (LWP 24333)]
    0xb7ec38b6 in LOF_Script::GetBool (this=0x8824120, inChan=0xb2e29d8,
    outVal=0xb68cba2b, inIdx=0, inRef=0xcf4ce70)
    at Function/Output/LOF_Script.cpp:85
    85 dSP ;


    any suggestion.

    thanks
    Gopi Manapragada
     
    satya manapragada, Oct 26, 2005
    #1
    1. Advertising

  2. Also sprach satya manapragada:

    > I am fairly new to embedding perl into c. I am trying to rewrite an
    > application. which has following code. This code used to work fine with
    > perl 5.0005 on BeOS, but when started to compile against perl 5.8.4 on
    > Linux, initially it complained unsatisfied variable my_perl variable so
    > i added "#include <XSUB.h> " header which solved it. But know when I
    > run it i get Segmentation fault.


    I bet that this 5.8.4 is a threaded perl. The difference to an
    unthreaded perl is that each function accessing perl's internal state
    variables (such as the perl stack) needs access to the thread-context.

    For convenience, there's a number of macros that you have to put in that
    take care of providing that thread-context.

    > here is code
    >
    > #include <EXTERN.h>
    > #include <perl.h>
    > #include <XSUB.h>
    >
    > .........
    > .........
    > .........
    > GetBool(
    > char *inChan,
    > bool *outVal,
    > int16 inIdx,
    > unsigned int *inRef)
    > {


    Change that to:

    GetBool(pTHX_
    char *inChan,
    bool *outVal,
    int16 inIdx,
    unsigned int *inRef)

    The 'pTHX_' is expanded differently depending on whether the perl is
    threaded or not. There's also 'pTHX' which is used for functions with no
    parameters.

    You also need to rewrite all calls to the above function:

    GetBool(inChan, outVal, inIdx, inRef);

    becomes

    GetBool(aTHX_ inChan, outVal, inIdx, inRef);

    There's also 'aTHX' for calling parameterless functions.

    pTHX/aTHX need to be added to every function that makes use of the perl
    API in one way or the other.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Oct 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinod

    Suprising seg fault!!!

    Vinod, Sep 27, 2003, in forum: C++
    Replies:
    3
    Views:
    490
    Ron Natalie
    Sep 27, 2003
  2. Corrine
    Replies:
    1
    Views:
    445
    Gianni Mariani
    Nov 27, 2003
  3. Manuel Maria Diaz Gomez

    fstream::open & seg fault

    Manuel Maria Diaz Gomez, Jul 20, 2004, in forum: C++
    Replies:
    3
    Views:
    1,313
    John Harrison
    Jul 20, 2004
  4. goktan
    Replies:
    4
    Views:
    2,044
    Howard
    Aug 2, 2004
  5. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    485
    Victor Bazarov
    Feb 27, 2005
Loading...

Share This Page