PreferredUserName has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Friends, Here's a try at embedding perl, taken mostly from perlembed. The problem is that each call to eval_pv() leaks about 387 bytes. Am I doing something wrong?
#include <EXTERN.h> #include <perl.h> int main(int argc, char **argv) { char *embedding[] = { "", "-e", "1" }; PerlInterpreter *interp = perl_alloc(); perl_construct(interp); if (perl_parse(interp, NULL, 3, embedding, (char **)NULL) != 0) { warn("perl_parse"); return -1; } if (perl_run(interp) != 0) { warn("perl_run"); return -1; } // leak repeatedly for (;;) eval_pv("1", TRUE); perl_destruct(interp); perl_free(interp); return 0; }

Replies are listed 'Best First'.
Re: eval_pv leaking
by Joost (Canon) on Dec 27, 2004 at 17:09 UTC
    From the perlapi manpage (emphasis mine):

    eval_pv Tells Perl to "eval" the given string and return an SV* result.

    I tried using
    SV* ret = eval_pv("1;", TRUE); SvREFCNT_dec(ret);
    but that didn't work (probably because eval_pv works "kindof like a subroutine" in that it returns a mortal SV* - or something; I confess ignorance of the subtleties in perlembedding) - so messed around with the code until I came up with this:
    for (;;) { ENTER; SAVETMPS; SV* ret = eval_pv("1;", TRUE); FREETMPS; LEAVE; }

    Which doesn't leak for me (perl, v5.8.5 built for i686-linux-thread-multi).

    update: this is not really documented AFAIK, probably because you're not likely to be calling eval_* all that much. The perlembed documentation should probably mention something about how to deal with the return value (as it does describe handling the various call_* parameters and return values).

      This works for me as well. Thanks!
Version info
by PreferredUserName (Pilgrim) on Dec 27, 2004 at 16:35 UTC
    Forgot to include version stuff. Here it is:
Re: eval_pv leaking
by demerphq (Chancellor) on Dec 27, 2004 at 16:40 UTC

    IMO you should redirect this question to P5P.

    ---
    demerphq