Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

MemLeak in Perl from C Calls

by stefan k (Curate)
on Apr 24, 2001 at 12:34 UTC ( [id://74985]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
I'm currently working on glueing C and perl together (maybe there will be no more fopen() and fscanf() *grin*)

The problem is that a) the bindings are hard to understand and b) my code leaks memory as hell. But I took almost everything more or less directly from man perlembed and man perlguts, thus it can't be _that_ wrong...
BTW: this is a cooked down version for you, fellow Monks, that doesn't leak as much as the original which has some SVs and stuff, but still ... it leaks :(

I post the code right below, but a quick word of warning: yes, it's C ;-)

/* compile with */ /* gcc -o monk monk.c `perl -MExtUtils::Embed -e ccopts -e ldopts` */ /* I tried this under Perl 5.6.0 and perl 5.005_3 */ # include <stdio.h> # include <EXTERN.h> # include <perl.h> # include <unistd.h> void call_perl(); int main(int argc, char **argv, char **env) { /* That's OK: */ printf("Testing perl embedding\n"); call_perl(); /* But in a loop we leak as hell */ while (1) { call_perl(); sleep(1); } } void call_perl(void) { static PerlInterpreter *my_perl; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); eval_pv("print \"Perl Version: $]\\n\"",TRUE); perl_destruct(my_perl); perl_free(my_perl); }

Regards Stefan K

Replies are listed 'Best First'.
Re: MemLeak in Perl from C Calls
by kal (Hermit) on Apr 24, 2001 at 14:39 UTC

    Stefan, have you tried PL_perl_destruct_level = 1; before the loop? It's a global, documented in perlembed.

      Thanks for your guess, but it's still growing and growing in top :((

      I tried it at the beginning of main and right before the while-statement...

      Any other ideas?

      Regards Stefan K

        No, as you say, you're using it pretty much as documented. Probably the best thing to do will be to keep the interpreter hanging around, and call perl_eval_ev() more than once - so in your loop, you get rid of the perl_construct/destruct, and put them outside the loop. That way, the memory you leak is constant, rather than proportional to the number of times you have looped. Not a fix, but a solution nonetheles...

Re: MemLeak in Perl from C Calls
by Anonymous Monk on Apr 24, 2001 at 18:41 UTC
    Hi Monks, We are also facing the same problem. The memory is growing like anything. The only difference is, we are using call_pv() to invoke the perl subroutine instead of perl_run(). We thought of posting the same question yesterday itself. However, we couldn't. When will the memory allocated by the perl be released, during perl_destruct() or when the reference count to perl variables (scalar/hash/list) becomes zero?. Any suggestions or comments will be helpful. Regards, Poornachandran SM
      Was there any resolution to this? I have been having the exact same problem.....
        Even after almost a decade, the problem exists as it was!
Re: MemLeak in Perl from C Calls
by ton (Friar) on Apr 24, 2001 at 21:27 UTC

    Update: Nevermind; I was answering the opposite question (calling C from Perl). Sorry

      Hey I had just checked the Inline-docs and wanted to point that out, when suddenly you edited your node *gg* Anyway: always thanks for the effort :)

      Regards Stefan K

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://74985]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found