rajiyer has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a small C++ application that invokes the Perl Interpreter and calls a perl function 2000 times. An array of strings is passed to the perl function, which pushes another value into the array. Here's the code:
Here's the Perl module (Test.pm), that is being invoked through the above program.#pragma warning(disable:4786) #pragma warning(disable:4788) #pragma warning(disable:4503) #include <vector> #include <string> #include <iostream> using namespace std; #include <EXTERN.h> #include <perl.h> #include <XSUB.h> typedef vector<string> V_S; void addValue(V_S& argVec) { dSP; ENTER; SAVETMPS; PUSHMARK(SP); AV* array = newAV(); for (int i = 0; i < argVec.size(); i++) { SV * value = newSVpvn( (char *)argVec[i].c_str(), argVec[i].length() ); av_push(array, value); } XPUSHs(newRV((SV*)array)); PUTBACK; perl_call_pv("Test::AddValue", G_EVAL|G_SCALAR); SPAGAIN; argVec.clear(); { I32 len = 0; while( av_len(array) >= len ) { SV ** v = av_fetch(array, len, FALSE ); SV * value = NULL; if( v ) { value = *v; int len = SvLEN(value); char * dataPtr = new char[len]; memcpy( dataPtr, SvPV_nolen(value), len ); argVec.push_back(string(dataPtr, len)); delete[] dataPtr; } sv_free(av_delete(array, len, FALSE)); len++; } } av_undef(array); PUTBACK; FREETMPS; LEAVE; } void main() { V_S myVals; myVals.push_back("value11"); myVals.push_back("value12"); static PerlInterpreter* my_perl; my_perl = perl_alloc(); perl_construct(my_perl); char* argv[] = {"", "F:\\Personal\\Misc Code\\PerlFromC\\Debug\\Test.pm"}; int status = perl_parse(my_perl, NULL, 1, argv, NULL); if (status != 0) { cout << "Error in perl_parse"; return; } for (int i = 0; i < 2000; i++) { V_S copyVec = myVals; addValue(copyVec); cout << "============== After calling Perl Module ================\n"; V_S::iterator vItr; for (vItr = copyVec.begin(); vItr != copyVec.end(); vItr++) cout << "Value = " << vItr->c_str() << "\n"; } perl_destruct(my_perl); perl_free(my_perl); }
package Test; require Exporter; @ISA = (Exporter); sub AddValue { $_[0]->[2] = "myvalue"; }
Any help with this would be highly appreciated.
Regards,
Rajalakshmi Iyer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Embedded Perl - Memory leak
by Molt (Chaplain) on Oct 16, 2003 at 10:39 UTC | |
by rajiyer (Acolyte) on Oct 16, 2003 at 10:48 UTC | |
by IlyaM (Parson) on Oct 16, 2003 at 11:58 UTC | |
by Elian (Parson) on Oct 16, 2003 at 16:43 UTC | |
|
Re: Embedded Perl - Memory leak
by ptkdb (Monk) on Oct 16, 2003 at 13:09 UTC | |
by rajiyer (Acolyte) on Oct 16, 2003 at 13:23 UTC |