All, I'm making calls to eval_pv in an application with an embedded interpreter. The final code was leaking memory, so I chopped it down to the point where I have only a call to eval_pv in a tight loop:
#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; main (int argc, char **argv, char **env) { STRLEN n_a; int i; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); for(i=0; i < 10000000; i++) { eval_pv("1", TRUE); } perl_destruct(my_perl); perl_free(my_perl); }
This doesn't create any variables in perl space (I don't think) and I throw away the SV* returned by eval_pv. Watching memory use as it runs, this still leaks memory compiled agains perl 5.8.3 and 5.6.1. As an experiment, I tried eval_sv with:
#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; main (int argc, char **argv, char **env) { STRLEN n_a; int i; SV* my_string; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); my_string = newSVpv("2", 0); for(i=0; i < 10000000; i++) { eval_sv(my_string, TRUE); } perl_destruct(my_perl); perl_free(my_perl); }
which leaks in the same way. However, if I use call_pv instead, this doesnt leak at all:
#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; main (int argc, char **argv, char **env) { STRLEN n_a; int i; char *embedding[] = { "", "-e", "sub fred {1;};" }; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); for(i=0; i < 10000000; i++) { dSP; ENTER; SAVETMPS; PUSHMARK(sp); call_pv("fred", G_DISCARD); SPAGAIN; LEAVE; } perl_destruct(my_perl); perl_free(my_perl); }
This seems fairly fundamental, so I must missing something. In my application, it is possible that eval_pv be called a large number of times (hundreds of millions). Any ideas? Many thanks!

In reply to memory leak in eval_pv? by lightspeed

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.