Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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