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

Anybody out there familiar with perlembed? I have playing with embedded perl in my C program and I am having a hard time to fix the memory leak problem.

I have the following piece of C test code:

#include <stdio.h> #include <unistd.h> #include <EXTERN.h> #include <perl.h> EXTERN_C void xs_init (pTHXo); EXTERN_C void boot_DynaLoader (pTHXo_ CV* cv); EXTERN_C void xs_init(pTHXo) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); } int main (int argc, char **argv) { PerlInterpreter *perl = NULL; int count = 0; // PL_perl_destruct_level = 1; fprintf(stdout, "Press Enter to start, Ctl-D to quit ..."); while (getchar() != EOF) { fprintf(stderr, "********COUNT = %d, DESTRUCT LEVEL(0) = %d\n" +, ++count, PL_perl_destruct_level); perl = perl_alloc(); perl_construct(perl); perl_parse(perl, xs_init, argc, argv, NULL); perl_run(perl); PL_perl_destruct_level = 1; perl_destruct(perl); perl_free(perl); perl = NULL; fprintf(stdout, "Press Enter to continue, Ctl-D to quit ..."); + } return 0; }

And I compile it like this:

gcc -o postv postversion.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

My testing Perl script is very simple:

my $string = "HELLO WORLD\n"; print $string;

What the program does is it will take in a Perl script file name and then stuck in a loop running the Perl script in C program. In every loop, a new interpreter will be allocated at the beginning and destroyed at the end.

I read in the perlembed manpage that if you set PL_perl_destruct_level to 1, the Perl interpreter will go away cleanly. However, this value seems to be reset everytime perl_construct() is being called, so I think the right place to set it is before the perl_destruct() is called and I tested that out and it seems correct.

I am running this happily with 5.6.0, but today I upgraded my Perl to 5.6.1 and if I run the same piece of code, the second time you construct the Perl interpreter, it will crash and a core dump is being generated. Then I tried to compile Perl with -Dusemultiplicty on, and it's even worse, it will crash as soon as the first interpreter is constructed.

Another problem I have having is that if in my testing Perl script, I am using some module, like :

my $string = "HELLO WORLD\n"; print $string; use Data::Dumper; print Dumper($string);

Even if I set PL_perl_destruct_level to 1, there will be huge memory leak. Any clue? How can I fix this problem? Actually this is the reason I upgrade to 5.6.1, I was trying to see if the latest code is better but turns out to be even worse.

So anyone out there has any clue on how to fix them? I want to have a C program that will occasionaly create a Perl interpreter and execute some script, go away cleanly and I wrote that test code to test out the embedded module.


Thanks a lot,


Jerry

Replies are listed 'Best First'.
Re: How to destroy embedded Perl interpreter cleanly (no memory leak)?
by JSchmitz (Canon) on Jun 05, 2002 at 12:56 UTC
    First off you should be calling perl_run so that your DESTROY methods and end blocks are executed at the right time. Have you looked at the simple demo of embedding that ships with Perl called miniperlmain.c?

    I also think that it is a better idea to maintain a single interpreter rather than creating and destroying them all the time. I think this would help out with speed a lot since Perl would only have to be loaded into memory once.

    cheers

    JSchmitz