I have a problem with a memory leak when calling a perl .pm file
from a C++ program.

I have pared down the C++ and .pm files to be bare bones.

The larry.pm file contains:

--------------------
package larry;

use strict;
use English;

sub larry
{
    my ($arg1, $arg2) = @_;
    print "In larry::larry, ARGS=$arg1, $arg2 \n";
}

---------------------------

The .cc file is attached.

When I run the program, I get a 4K byte memory leak for each
14 iterations in the loop, where each iteration just invokes
my simple larry.pm above.

Does anyone know why, and what I can do to fix this?

I'm also open to alternate ways of calling the .pm script,
as I am no expert in this area.

Also, the destroyPerl() function in the attached cc file does
not seem to fully clean up when I use it.  Do I need to do
more?

Thanks very much to anyone who can help me.

 larry

        ------------------------------------------------------
        Larry Bressler                  [EMAIL PROTECTED]
        LB Resources, Inc.              617.964.5686
// larry.cc - program to test memory leak in embedded Perl.

#define EXTERN_C extern "C"
#include <EXTERN.h>               /* from the Perl distribution     */
#include <perl.h>                 /* from the Perl distribution     */

#include <string>

EXTERN_C void boot_DynaLoader(CV* cv);


// This function is used as "glue" when initializing the Perl interpreter.
// It causes the DynaLoader module to be linked in so that Perl extensions
// can be dynamically loaded.
static void xs_init()
{
  newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
}

PerlInterpreter* perl_ = NULL;

main() 
{

  int numArgs = 0;

  cout << "Starting test program.\n";

  char *cmdline[10];

  // Construct perl initialization arguments.
  // Arguments are -w -I. -e 0

  cmdline[numArgs++] = "";      // First arg is always empty
  cmdline[numArgs++] = "-w";    // Turn on Perl warnings
            
  // Directory to search for Perl modules.
  cmdline[numArgs++] = "-I.";

  // Execute null script at initialization time.
  cmdline[numArgs++] = "-e";
  cmdline[numArgs++] = "0";

  // Now allocate and initialize Perl interpreter.
  perl_ = perl_alloc();
  perl_construct(perl_);
  perl_parse(perl_, xs_init, numArgs, cmdline, NULL);
  perl_run(perl_);

  // Create a script that looks like:
  //    "use module; eval { module::command(arg1, arg2 ...); };".

  string script = "use larry; eval { larry::larry( 'hello', 'world'); }; print $@ if 
($@);";

  cout << "Running perl script: " << script.c_str() << "\n";

  // Evaluate the perl script:
  // Problem!!! - Every 16 of these cause a leak of 4K bytes, as
  //   monitored by Linux top utility.
  for (int i=0; i < 1000; i++) {
    cout << "\nTest #" << i << "\n";
    perl_eval_pv(script.c_str(), FALSE);
    sleep(1);
  }

  cout << "All done.\n";
}

// This destroy does not seem to clean up everything.  Am I missing
// anything ????
void destroyPerl()
{
  // Set PL_perl_destruct_level to 1 to make sure everything gets cleaned up
  // when perl_destruct is called.

  // !!!! This line causes a core dump on my system, if called. 
  //      Do I need it ????
  // PL_perl_destruct_level = 1;

  perl_destruct(perl_);
  perl_free(perl_);
  perl_ = NULL;
}

Reply via email to