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.


In reply to Embedded Perl - Memory leak by rajiyer

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.