Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

perl_free problem

by iladyko_vision (Initiate)
on Mar 12, 2007 at 11:10 UTC ( #604319=perlquestion: print w/replies, xml ) Need Help??

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

Hello, all

I have a problem with memory reallocation when using a Perl interpreter embeded in my application. (I use few interpreters in few threads. For each interpreter exists its own thread.)

It seems that if we create more than one interpreter we must delete them all at one place.

Flat example:
0: 1: int main(int argc, char **argv) 2: { 3: PerlInterpreter *one_perl, *two_perl; 4: char *one_args[] = { "one_perl", "-e", "print qq(Hi, I'm $^X\n +)" }; 5: char *two_args[] = { "two_perl", "-e", "print qq(Hi, I'm $^X\n +)" }; 6: 7: PERL_SYS_INIT3(&argc,&argv,&env); 8: one_perl = perl_alloc(); 9: 10: PERL_SET_CONTEXT(one_perl); 11: perl_construct(one_perl); 12: 13: PERL_SET_CONTEXT(one_perl); 14: perl_parse(one_perl, NULL, 3, one_args, (char **)NULL); 15: 16: PERL_SET_CONTEXT(one_perl); 17: perl_run(one_perl); 18: 19: PERL_SET_CONTEXT(one_perl); 20: perl_destruct(one_perl); 21: 22: PERL_SET_CONTEXT(one_perl); 23: perl_free(one_perl); 24: 25: two_perl = perl_alloc(); 26: PERL_SET_CONTEXT(two_perl); 27: perl_construct(two_perl); 28: PERL_SET_CONTEXT(two_perl); 29: perl_parse(two_perl, NULL, 3, two_args, (char **)NULL); 30: PERL_SET_CONTEXT(two_perl); 31: perl_run(two_perl); 32: //PERL_SET_CONTEXT(one_perl); 33: //perl_free(one_perl); 34: PERL_SET_CONTEXT(two_perl); 35: perl_destruct(two_perl); 36: PERL_SET_CONTEXT(two_perl); 37: perl_free(two_perl); 38: 39: PERL_SYS_TERM(); 40: 41: return 0; 42:}

Exception is thrown in 29 line. If we comment 22, 23 line and uncomment 32 and 33 lines, then all will be OK.

Questions: Please, point me to error in the code or propose method to delete interpreters separately.

Replies are listed 'Best First'.
Re: perl_free problem
by zentara (Archbishop) on Mar 12, 2007 at 13:03 UTC
    Your code runs fine here as posted. For anyone who wants to try it, you need to add
    #include <EXTERN.h> #include <perl.h>
    as headers and compile with
    cc -o perl-interp perl-interp.c `perl -MExtUtils::Embed -e ccopts -e + ldopts`
    Furthermore, I changed the args to be more descriptive:
    char *one_args[] = { "one_perl", "-e", "print qq(Hi, I'm 1\n)" }; + char *two_args[] = { "two_perl", "-e", "print qq(Hi, I'm 2\n)" };
    Maybe you could say what platform, Perl version, and compiler you are using? I'm using linux, Perl 5.8.8 , and gcc 4.1.1

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      1. I included this headers (forget post them last time).

      #include <EXTERN.h> #include <perl.h>

      2. I don't understand what it means, because I use cl compiler.

      cc -o perl-interp perl-interp.c `perl -MExtUtils::Embed -e ccopts -e + ldopts

      As I understand "perl -MExtUtils::Embed -e ccopts -e ldopts" are command arguments. But it doesn't help.

      I'd added more information about my problem. Here it is:

      ------ platform - win32 (WinXP).

      ------ Memory protection - Paging

      ----- I use VC.NET 2003.

      ------ Output of 'perl -V'

      Summary of my perl5 (revision 5 version 9 subversion 4) configuration: Platform: osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef useithreads=define, usemultiplicity=define useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=und +ef use64bitint=undef, use64bitall=undef, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cl', ccflags ='-nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D +_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPER +L_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX', optimize='-MD -Zi -DNDEBUG -O1', cppflags='-DWIN32' ccversion='13.10.3077', gccversion='', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64 +', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='link', ldflags ='-nologo -nodefaultlib -debug -opt:ref,icf -l +ibpath:"c:\perl\lib\CORE" -machine:x86' libpth=\lib libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib + comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netap +i32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32 +.lib msvcrt.lib perllibs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool +.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib n +etapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32 +.lib msvcrt.lib libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl59.lib gnulibc_version='' Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -debug -opt: +ref,icf -libpath:"c:\perl\lib\CORE" -machine:x86' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_ITHREADS USE_LARGE_FILES USE_PERLIO Built under MSWin32 Compiled at Mar 12 2007 14:54:36 @INC: D:/Irina/3rdparty/perl/lib .


      Run form VC 2003

      Debug tracing:

      Unhandled exception at 0x7c901230 in perlInterpreter.exe: User breakpoint.


      Debug runnng (Ctrl+F5) output:

      Hi, I'm 1.

      Hi, I'm 2.

      Attempt to free unreferenced glob pointers, Perl interpreter: 0x8623b4 during global destruction.

      Unbalanced string table refcount: (1) for -e during global destruction.


      Run from FAR output:

      Hi, I'm 1

      Hi, I'm 2

      Free to wrong pool 882c40 not 882c3f during global destruction.

      ------ Full code:

      #include "stdafx.h"//empt #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; #define SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)" int main(int argc, char **argv) { PerlInterpreter *one_perl, *two_perl; char *one_args[] = { "one_perl", SAY_HELLO }; char *two_args[] = { "two_perl", SAY_HELLO }; PERL_SYS_INIT3(&argc,&argv,&env); one_perl = perl_alloc(); my_perl = one_perl; PERL_SET_CONTEXT(one_perl); perl_construct(one_perl); PERL_SET_CONTEXT(one_perl); perl_parse(one_perl, NULL, 3, one_args, (char **)NULL); PERL_SET_CONTEXT(one_perl); perl_destruct(one_perl); PERL_SET_CONTEXT(one_perl); perl_free(one_perl); two_perl = perl_alloc(); my_perl = two_perl; PERL_SET_CONTEXT(two_perl); perl_construct(two_perl); PERL_SET_CONTEXT(two_perl); perl_parse(two_perl, NULL, 3, two_args, (char **)NULL); PERL_SET_CONTEXT(two_perl); perl_destruct(two_perl); PERL_SET_CONTEXT(two_perl); perl_free(two_perl); PERL_SYS_TERM(); return 0; }
      ------ Stack:
      ntdll.dll!7c901230() ntdll.dll!7c96c943() ntdll.dll!7c95db9c() ntdll.dll!7c96cd11() ntdll.dll!7c96df66() ntdll.dll!7c926abe() ntdll.dll!7c94a5d0() ntdll.dll!7c94a5d0() ntdll.dll!7c9268ad() ntdll.dll!7c9268ad() msvcrt.dll!77c1c2de() ntdll.dll!7c926abe() ntdll.dll!7c9268ad() ntdll.dll!7c9268ad() ntdll.dll!7c9268ad() > perl59.dll!VMem::FreeLock() Line 237 C++ perl59.dll!VMem::Free(void * pMem=0x00860000) Line 222 + 0x8 + C++ perl59.dll!PerlMemFree(IPerlMem * piPerl=0x00860000, void * pt +r=0x00860000) Line 306 C++ perl59.dll!Perl_safesysfree(void * where=0x0012fbb8) Line 250 + + 0xe C perl59.dll!Perl_sv_clear(interpreter * my_perl=0x00860000, sv +* sv=0x00000000) Line 5122 C msvcrt.dll!77c1c2de() msvcrt.dll!77c1c2de() msvcrt.dll!77c1c2de() perl59.dll!VMem::Free(void * pMem=0x008a4cac) Line 222 C++ perl59.dll!CPerlHost::FreeShared(void * ptr=0x008a4cac) Line +101 C++ perl59.dll!PerlMemSharedFree(IPerlMem * piPerl=0x00882c18, voi +d * ptr=0x008a4cac) Line 360 C++ perl59.dll!perl_destruct(interpreter * my_perl=0x00862bfc) Li +ne 1058 + 0x12 C perlInterpreter.exe!main(int argc=1, char * * argv=0x00893d80) + Line 390 + 0x9 C++ perlInterpreter.exe!mainCRTStartup() Line 259 + 0x19 C kernel32.dll!7c816d4f() kernel32.dll!7c8399f3()
      I hope on your help.
        2. I don't understand what it means, because I use cl compiler. cc -o perl-interp perl-interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

        The command-line arguments (as you call them ) are actually backticks and expand out into the needed includes and libs needed by your Perl version. On linux/unix style systems, it is done all the time, look at gtk's pkg-config system.

        Anyways, on my system, the bacticks expand out to

        -Wl,-E -L/usr/local/lib /usr/lib/perl5/5.8.8/i686-linux-thread-multi/ +auto/DynaLoader/DynaLoader.a -L/usr/lib/perl5/5.8.8/i686-linux-thread +-multi/CORE -lperl -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthrea +d -lc -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing - +pipe -Wdeclaration-after-statement -I/usr/local/include -D_LARGEFILE_ +SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl5/5.8.8/i686-linux-thre +ad-multi/CORE
        As you can see, using the backticks, is far easier than manually trying to find all those system-specific file locations.

        And sorry, I don't use windows, so I'm as lost as you as far as win32 goes.

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: perl_free problem
by osunderdog (Deacon) on Mar 12, 2007 at 14:22 UTC

    You might want to check out this thread: Using Perl's Malloc

    Also, it would probably be helpful for you to mention the OS, Perl version, compiler, allocation method, etc.

    Hazah! I'm Employed!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://604319]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-01-24 09:32 GMT
Find Nodes?
    Voting Booth?