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

Now, I have two modules: one in perl the other using xs to call Windows API function.

When the script is run, it register a callback function and listen to keystrokes.

It work, except that after x keystrokes (x can be large as 50 ...) perl crash, sometime with an Out of memory! message, sometime without message.

I have no experience with using the debugger.

Perl -V says

ummary of my perl5 (revision 5 version 24 subversion 0) configuration: Platform: osname=MSWin32, osvers=6.3, archname=MSWin32-x86-multi-thread-64in +t uname='Win32 strawberry-perl 5.24.0.1 #1 Tue May 10 17:10:11 2016 +i386' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef useithreads=define, usemultiplicity=define use64bitint=define, use64bitall=undef, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags =' -s -O2 -DWIN32 -DPERL_TEXTMODE_SCRIPTS -DPER +L_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fwrapv -fno-strict-aliasing - +mms-bitfields', optimize='-s -O2', cppflags='-DWIN32' ccversion='', gccversion='4.9.2', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678 +, doublekind=3 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2, longdblkind=3 ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='lo +ng long', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='g++', ldflags ='-s -L"C:\STRAWB~1\perl\lib\CORE" -L"C:\STRAWB~ +1\c\lib"' libpth=C:\STRAWB~1\c\lib C:\STRAWB~1\c\i686-w64-mingw32\lib C:\STR +AWB~1\c\lib\gcc\i686-w64-mingw32\4.9.2 libs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 +-ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -l +mpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 perllibs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdl +g32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_3 +2 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 libc=, so=dll, useshrplib=true, libperl=libperl524.a gnulibc_version='' Dynamic Linking: dlsrc=dl_win32.xs, dlext=xs.dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-mdll -s -L"C:\STRAWB~1\perl\lib\CORE" +-L"C:\STRAWB~1\c\lib"' Characteristics of this binary (from libperl): Compile-time options: HAS_TIMES HAVE_INTERP_INTERN MULTIPLICITY PERLIO_LAYERS PERL_COPY_ON_WRITE PERL_DONT_CREATE_GVSV PERL_HASH_FUNC_ONE_AT_A_TIME_HARD PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS PERL_MALLOC_WRAP PERL_PRESERVE_IVUV USE_64_BIT +_INT USE_ITHREADS USE_LARGE_FILES USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE USE_LOCALE_NUMERIC USE_LOCALE_TIME USE_PERLIO USE_PERL_ATOF Built under MSWin32 Compiled at May 10 2016 17:20:50 %ENV: PERL5LIB="U:\docs\perl\lib" PERL_CPANM_HOME="C:\Users\rappazf\AppData\Local\Perl\.cpanm" @INC: U:\docs\perl\lib C:/strawberry/perl/site/lib/MSWin32-x86-multi-thread-64int C:/strawberry/perl/site/lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .
Thanks for any advice ... frazap

Replies are listed 'Best First'.
Re: perl +xs : crash after some time
by stevieb (Canon) on Nov 03, 2017 at 13:36 UTC

    Look into running your app under valgrind. It's the first thing I turn to when I experience crashes or other issues with my C/XS code.

    update: Completely overlooked this is Windows here, and valgrind doesn't work there. However, there may be an alternative. See this SO post for example.

    Another option, if you're on win10, using the windows bash thingy.

      I'm on Windows 10, but without the anniversary update (and I have no options on update plan)

      In fact if I run the code given here Re^6: Win32::API and keyboard hook, I can crash perl after some/many keystrokes.

      The error messages is emit at the call of the perl sub:

      Attempt to change a readonly variable

      or

      Odd number of elements in hash assignment

      I pass a sub reference to be called by the c hook function, and it's the code that receive the parameters from the c function that crash:

      sub { my ($cup, $code, $alt, $ext) = @_; .... }

        Code looks nice and contained in your example. I'll try to get a chance to poke around over the course of the weekend and see if I can reproduce/come up with a debugging procedure.

        -stevieb

Re: perl +xs : crash after some time
by Anonymous Monk on Nov 04, 2017 at 09:39 UTC

    Reading perlcall, there's an example with SPAGAIN; after the call_pv().

    The purpose of the macro "SPAGAIN" is to refresh the local copy of the stack pointer. This is necessary because it is possible that the memory allocated to the Perl stack has been reallocated during the call_pv call.
    You do the opposite: a PUTBACK; after the call. There shouldn't be need for either as you don't access SP after the call.

    Anyway, that was just my quick guess. Can you try the code without this (second) PUTBACK; statement?

      Yes, You're right !

      reading again perlcall, passing one parameter here there's no call to PUTBACK after call_pv.

      Without this second PUTBACK, I was not able to crash my code with a few minutes testing

      Thanks a lot !

      François