Hi *.

This question is not exactly about perl but maybe your trancendental experience allows you to travel to other spheres as well...

I have to maintain a massivly multithreaded C++ application which uses embedded perl to allow for data modification in certain places.
This does work in general, but every now and then the application crashes although the same perl code has been run for a day or more.
Currently it is implemented as follows:

When a new perl script is to be loaded the following code is executed, allocating a new perl interpreter used solely for this script:
void * new_perlFilter( char * pcScriptName ) { int _iArgc = 2; char* _ppcArgv[] = { "", NULL, NULL }; _ppcArgv[1] = pcScriptName; pthread_mutex_lock(&cloneMux); PerlInterpreter * ppi = perl_alloc(); PERL_SET_CONTEXT( ppi ); perl_construct( ppi ); if( perl_parse( ppi, xs_init, _iArgc, _ppcArgv, (char **)NULL ) ) { ppi = NULL; } pthread_mutex_unlock(&cloneMux); return ppi; }

There is a global, mutex-protected list of these instances and when the respective script is needed, this code is executed, calling a sub "my_handler" in that script (with pPi being the interpreter instance from the list):
char * run_sub( void * pPi, tPerlCallMap* pCallMap) { char* _pcRet = NULL; try { if(pPi) { STRLEN _lLength = 0; SV* _pSV = NULL; char* _pcErg = NULL; int _iStat = 0; pthread_mutex_lock(&cloneMux); PERL_SET_CONTEXT( (PerlInterpreter*)pPi ); PerlInterpreter* ppicl = perl_clone((PerlInterpreter*)pPi, CLONEf_COPY_STACKS); pthread_mutex_unlock(&cloneMux); PerlInterpreter* my_perl = ppicl;// nec. for dSP PERL_SET_CONTEXT( my_perl ); dSP ; ENTER ; SAVETMPS ; PUSHMARK(SP) ; tPerlCallMap::iterator _oItr; for(_oItr = pCallMap->begin(); _oItr != pCallMap->end(); _oItr++) { SV* _pSVIn = newSVpv(_oItr->second.c_str(), 0); SvUTF8_on(_pSVIn); XPUSHs(sv_2mortal(_pSVIn)); } PUTBACK ; _iStat = call_pv("my_handler", G_SCALAR | G_EVAL | G_KEEPERR); if(_iStat) { SPAGAIN; _pSV = POPs; SvUTF8_on(_pSV); _pcErg = SvPV(_pSV, _lLength); long _lErgLength = strlen(_pcErg); _pcRet = new char[_lErgLength + 1]; if( _pcRet != NULL ) { _pcRet[_lErgLength] = 0; strcpy( _pcRet, _pcErg); } PUTBACK; } FREETMPS ; LEAVE ; PERL_SET_CONTEXT( my_perl ); perl_destruct( my_perl ); perl_free( my_perl ); } } catch(int nWhy) {} catch(...) {} return _pcRet; }

I donīt see where the problem comes in.
The script runs ok for a high number of invocations, there is no "exit" in it and a "die" should be catched by the G_EVAL flag...

Besides my missing insight in the problem Iīm wondering if there isnīt a better approach to embedding perl:

Any hints are welcome!
heiko

In reply to Embedded Perl: Help needed by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.