Hi,
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:
#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);
}
Here's the Perl module (Test.pm), that is being invoked through the above
program.
package Test;
require Exporter;
@ISA = (Exporter);
sub AddValue
{
$_[0]->[2] = "myvalue";
}
This program works fine as such. But if you monitor it through the Windows
task manager, the memory consumption keeps increasing. It is stable
initially upto some point, beyond which it keeps rising. I tried using
Rational Purify (leak detection tool) with Perl source code, but still did
not get any pointers.
Any help with this would be highly appreciated.
Regards,
Rajalakshmi Iyer.