valgrind was reporting a memory leak in texi2any, like:

==3404772== 364 bytes in 1 blocks are definitely lost in loss record 128 of 151
==3404772==    at 0x4848899: malloc (in 
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3404772==    by 0x498D11F: newlocale (newlocale.c:199)
==3404772==    by 0x2B6D32: ??? (in /usr/bin/perl)
==3404772==    by 0x2B79BF: Perl_set_numeric_standard (in /usr/bin/perl)
==3404772==    by 0x2BA4DE: Perl_setlocale (in /usr/bin/perl)
==3404772==    by 0x61BF01B: ??? (in 
/usr/lib/x86_64-linux-gnu/perl-base/auto/POSIX/POSIX.so)
==3404772==    by 0x21BAD6: Perl_pp_entersub (in /usr/bin/perl)
==3404772==    by 0x203AC5: Perl_runops_standard (in /usr/bin/perl)
==3404772==    by 0x17B918: Perl_call_sv (in /usr/bin/perl)
==3404772==    by 0x17CA81: Perl_call_list (in /usr/bin/perl)
==3404772==    by 0x167018: ??? (in /usr/bin/perl)
==3404772==    by 0x168BEA: Perl_newATTRSUB_x (in /usr/bin/perl)

This was with perl 5.34.0 for x86_64-linux-gnu-thread-multi (on
Linux Mint 21.2).

This backtrace didn't show where the problem was, but by a process of
trial and error, inserting croak statements into the XS code at various
places, I tracked it down to the call to switch_to_global_locale in
translate_string in main/translations.c.

diff --git a/tp/Texinfo/XS/main/translations.c 
b/tp/Texinfo/XS/main/translations.c
index de4ed82e03..4659da9afa 100644
--- a/tp/Texinfo/XS/main/translations.c
+++ b/tp/Texinfo/XS/main/translations.c
@@ -185,6 +185,8 @@ translate_string (OPTIONS *options, const char * string,
      uses per thread locale, the result is unpredictable.  So we switch to
      global locales as we use setlocale */
   call_switch_to_global_locale ();
+  croak_message ("after switch_to_global_locale");
+
 
   /*  
   We need to set LC_MESSAGES to a valid locale other than "C" or "POSIX"

Adding the line there gives the leak, before call_switch_to_global_locale
doesn't.

(croak rather than exit, etc. as it allows Perl to clean up its allocations,
especially with a "destruct level" set)

There may have been a similar leak report made when xspara.c used to
call switch_to_global_locale.

I haven't tested it with any newer versions of Perl, but at least this
doesn't appear to be a bug in texi2any's code.  I am going to try to
add this to the suppressions file tp/texi2any.supp because we have
investigated it, so valgrind remains useful for checking for memory
leaks that we do have to fix.

(It could have been easier to track down if I compiled Perl with some
debugging information, I don't know.)


Reply via email to