I'm trying to find a Perl embedding solution without getting all memory grabbed by the process itself; in this process, I've isolated the very few code lines that really make me go nuts.
Consider the following bare-bones program, which actually doesn't try to do nothing really interesting but setting the same Perl variable one million times.
#include <EXTERN.h> #include <perl.h> #include <unistd.h> #include <stdio.h> void do_something () { dSP; ENTER; SAVETMPS; PUSHMARK(SP); eval_pv("$a = 1", 1); SPAGAIN; FREETMPS; LEAVE; } int main (int argc, char* argv[]) { PerlInterpreter *my_perl = perl_alloc(); char *embedding[] = {"", "-e", "0"}; I32 i; char c; perl_construct(my_perl); perl_parse(my_perl, NULL, 3, embedding, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; printf("Press a key to start\n"); read(0, &c, 1); for (i = 0; i < 1000000; ++i) do_something(); printf("Press a key to continue\n"); read(0, &c, 1); PL_perl_destruct_level = 1; perl_destruct(my_perl); perl_free(my_perl); printf("Press a key to exit\n"); read(0, &c, 1); return 0; }
What I really don't understand is why the process keeps growing demanding more memory as the index variable i increases.

After a suggestion from Joost (see below his response), I ended up finding that the following version of do_something shows less leakage (but not zero):

void do_something () { dSP; ENTER; SAVETMPS; eval_pv("$a = 1", 1); FREETMPS; LEAVE; }
The only thing that has popped to my mind is that maybe the return value from eval_pv, which is actually an SV*, could be the source of my problems, but I tried to either mark it as mortal or decrease its reference count with the only result of getting an error message about disposing of a variable without references.

What am I missing (apart from memory)?

Thank you in advance,

-- Flavio


In reply to Bare-bones embedding and memory leakage by polettix

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.