Seg Fault

S

satya manapragada

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
 
T

Tassilo v. Parseval

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top