zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: Free stuff properly in zsh/pcre module
@ 2018-01-07 14:09 Mikael Magnusson
  2018-01-08 12:29 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 2+ messages in thread
From: Mikael Magnusson @ 2018-01-07 14:09 UTC (permalink / raw)
  To: zsh-workers

Fix a few memleaks of the hints structure, also make sure to free all
the allocated stuff when the module is unloaded.

---

If you run
% zmodload zsh/pcre; pcre_compile hey; repeat 1000000; do pcre_study; done
then zsh mem usage goes up by about 100MB each time.

 Src/Modules/pcre.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c
index 659fd22d59..15ee34bc8f 100644
--- a/Src/Modules/pcre.c
+++ b/Src/Modules/pcre.c
@@ -88,10 +88,19 @@ bin_pcre_compile(char *nam, char **args, Options ops, UNUSED(int func))
     if (zpcre_utf8_enabled())
 	pcre_opts |= PCRE_UTF8;
 
-    pcre_hints = NULL;  /* Is this necessary? */
+#ifdef HAVE_PCRE_STUDY
+    if (pcre_hints)
+#ifdef PCRE_CONFIG_JIT
+	pcre_free_study(pcre_hints);
+#else
+	pcre_free(pcre_hints);
+#endif
+    pcre_hints = NULL;
+#endif
 
     if (pcre_pattern)
 	pcre_free(pcre_pattern);
+    pcre_pattern = NULL;
 
     target = ztrdup(*args);
     unmetafy(target, &target_len);
@@ -128,6 +137,14 @@ bin_pcre_study(char *nam, UNUSED(char **args), UNUSED(Options ops), UNUSED(int f
 	return 1;
     }
     
+    if (pcre_hints)
+#ifdef PCRE_CONFIG_JIT
+	pcre_free_study(pcre_hints);
+#else
+	pcre_free(pcre_hints);
+#endif
+    pcre_hints = NULL;
+
     pcre_hints = pcre_study(pcre_pattern, 0, &pcre_error);
     if (pcre_error != NULL)
     {
@@ -528,5 +545,21 @@ cleanup_(Module m)
 int
 finish_(UNUSED(Module m))
 {
+#if defined(HAVE_PCRE_COMPILE) && defined(HAVE_PCRE_EXEC)
+#ifdef HAVE_PCRE_STUDY
+    if (pcre_hints)
+#ifdef PCRE_CONFIG_JIT
+	pcre_free_study(pcre_hints);
+#else
+	pcre_free(pcre_hints);
+#endif
+    pcre_hints = NULL;
+#endif
+
+    if (pcre_pattern)
+	pcre_free(pcre_pattern);
+    pcre_pattern = NULL;
+#endif
+
     return 0;
 }
-- 
2.15.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: PATCH: Free stuff properly in zsh/pcre module
  2018-01-07 14:09 PATCH: Free stuff properly in zsh/pcre module Mikael Magnusson
@ 2018-01-08 12:29 ` Sebastian Gniazdowski
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Gniazdowski @ 2018-01-08 12:29 UTC (permalink / raw)
  To: Mikael Magnusson, zsh-workers

On 7 stycznia 2018 at 15:09:39, Mikael Magnusson (mikachu@gmail.com) wrote:
> Fix a few memleaks of the hints structure, also make sure to free all
> the allocated stuff when the module is unloaded.
>  
> ---
>  
> If you run
> % zmodload zsh/pcre; pcre_compile hey; repeat 1000000; do pcre_study; done
> then zsh mem usage goes up by about 100MB each time.

Maybe Valgrind automatic tests can be integrated for next Zsh release? I've updated the README:

https://github.com/zdharma/VATS-zsh

It much helps to track things like the above reported leaks. Today, I was extending a module (an external one) and by logic, the changes couldn't cause any error or leak, but I just ran 7 test-sets, in 5 minutes (excluding the time devoted to updates needed for optimized Zsh build, which was skipping some function calls), and was done.

-- 
Sebastian Gniazdowski 
psprint /at/ zdharma.org 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-01-08 12:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-07 14:09 PATCH: Free stuff properly in zsh/pcre module Mikael Magnusson
2018-01-08 12:29 ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).