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

Hi everyone, Calling a subroutine from a regular expression causes a crash when I then try to create/join a thread. Looks like this messes up some internal stuff. Here is the code:
#!/usr/bin/perl use threads; use Thread::Semaphore; sub bad{ sub mysub{ print "my sub : $1 $2\n"; } $a="Hi there and hi there.\n"; $a =~ s/(\w+ \w+)(?{mysub();})//gi ; } sub mythread { print "begin\n"; print "end\n"; } bad(); my $thr = threads->new(\&mythread); @r=$thr ->join(); __END__
The output is:
./testthread.pl my sub : Hi there my sub : and hi begin end *** glibc detected *** /usr/bin/perl: double free or corruption (!prev +): 0x08271ce8 ***
Does anyone know what's wrong with using a regex this way ? If found it very convenient to process every pattern matching my regex, but I suppose I'll have to do it another way. Thank you.

Replies are listed 'Best First'.
Re: Thread crash
by gam3 (Curate) on Nov 21, 2007 at 17:16 UTC
    I have verified that your code crashed perl v5.8.8.
    This code does seem to run however.
    #!/usr/bin/perl use threads; use Thread::Semaphore; sub bad { sub mysub { print "my sub : $1 $2\n"; } $a = "Hi there and hi there.\n"; $a =~ s/(\w+ \w+)(?{mysub();})//gi ; } sub mythread { print "begin\n"; print "end\n"; } my $thr = threads->new(\&bad); @r=$thr ->join(); my $thr = threads->new(\&mythread); @r=$thr ->join(); __END__
    Also $a =~ s/(\w+ \w+)/&mysub()/ge; seems to give the same output as your code and also does not seem to cause the crash. You do need to worry about the output of the function though.
    #!/usr/bin/perl use threads; use Thread::Semaphore; sub mythread { print "begin\n"; print "end\n"; } sub mysub { print "my sub : $1 $2\n"; ''; } $a = "Hi there and hi there.\n"; $a =~ s/(\w+ \w+)/&mysub()/ge; my $thr = threads->new(\&mythread); @r=$thr ->join(); __END__
    -- gam3
    A picture is worth a thousand words, but takes 200K.
      Thank you very much for your fast replies. This is perfect for what I need to do, but I was not aware of this syntax. I'd be interested though in knowing if the crash was caused by my weird code (I'm newbie to Perl) or by an interpreter problem.
        Your weird code found an interpreter problem.
        -- gam3
        A picture is worth a thousand words, but takes 200K.
Re: Thread crash
by Fletch (Bishop) on Nov 21, 2007 at 17:12 UTC

    Threading in perl has been (and still is to some degree, AFAIK) kinda flakey. It'd help if you posted or appended the output from perl -V to give people an idea of what version of perl you're experiencing this with.

      Here it is :
      Summary of my perl5 (revision 5 version 8 subversion 8) configuration: Platform: osname=linux, osvers=2.6.15.7, archname=i486-linux-gnu-thread-mult +i uname='linux palmer 2.6.15.7 #1 smp thu sep 7 19:42:20 utc 2006 i6 +86 gnulinux ' config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dccc +dlflags=-fPIC -Darchname=i486-linux-gnu -Dprefix=/usr -Dprivlib=/usr/ +share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvend +orlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/ +local -Dsitelib=/usr/local/share/perl/5.8.8 -Dsitearch=/usr/local/lib +/perl/5.8.8 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/ma +n3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man +3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs - +Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.8 -Dd_ +dosuid -des' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemulti +plicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS +-DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE +_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2', cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN +-fno-strict-aliasing -pipe -I/usr/local/include' ccversion='', gccversion='4.1.3 20070831 (prerelease) (Ubuntu 4.1. +2-16ubuntu1)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt perllibs=-ldl -lm -lpthread -lc -lcrypt libc=/lib/libc-2.6.1.so, so=so, useshrplib=true, libperl=libperl.s +o.5.8.8 gnulibc_version='2.6.1' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_ITHREAD +S USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API Built under linux Compiled at Sep 29 2007 05:57:38 @INC: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl