[hackers] [dwm][PATCH] Fix memory leak

From: Isaac Canales <icanalesm0500_AT_gmail.com>
Date: Sun, 17 Jun 2018 18:32:17 +0200

- Free color scheme pointers
- Free fontset pointer
---
 drw.c | 23 +++++++++++++++++++++++
 drw.h |  2 ++
 dwm.c |  4 +++-
 3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/drw.c b/drw.c
index c638323..b446203 100644
--- a/drw.c
+++ b/drw.c
_AT_@ -191,6 +191,16 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
 		die("error, cannot allocate color '%s'", clrname);
 }
 
+void
+drw_clr_free(Drw *drw, Clr *clr)
+{
+	if (!drw || !clr)
+		return;
+
+	XftColorFree(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
+	             DefaultColormap(drw->dpy, drw->screen), clr);
+}
+
 /* Wrapper to create color schemes. The caller has to call free(3) on the
  * returned color scheme when done using it. */
 Clr *
_AT_@ -208,6 +218,19 @@ drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
 	return ret;
 }
 
+void
+drw_scm_free(Drw *drw, Clr *scm, size_t clrcount)
+{
+	size_t i;
+
+	if (!drw || !scm)
+		return;
+
+	for (i = 0; i < clrcount; i++)
+		drw_clr_free(drw, &scm[i]);
+	free(scm);
+}
+
 void
 drw_setfontset(Drw *drw, Fnt *set)
 {
diff --git a/drw.h b/drw.h
index 4bcd5ad..96a17f6 100644
--- a/drw.h
+++ b/drw.h
_AT_@ -39,7 +39,9 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in
 
 /* Colorscheme abstraction */
 void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
+void drw_clr_free(Drw *drw, Clr *clr);
 Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
+void drw_scm_free(Drw *drw, Clr *scm, size_t clrcount);
 
 /* Cursor abstraction */
 Cur *drw_cur_create(Drw *drw, int shape);
diff --git a/dwm.c b/dwm.c
index c0ef39a..a1057e1 100644
--- a/dwm.c
+++ b/dwm.c
_AT_@ -486,7 +486,9 @@ cleanup(void)
 	for (i = 0; i < CurLast; i++)
 		drw_cur_free(drw, cursor[i]);
 	for (i = 0; i < LENGTH(colors); i++)
-		free(scheme[i]);
+		drw_scm_free(drw, scheme[i], 3);
+	free(scheme);
+	drw_fontset_free(drw->fonts);
 	XDestroyWindow(dpy, wmcheckwin);
 	drw_free(drw);
 	XSync(dpy, False);
-- 
2.16.4
Received on Sun Jun 17 2018 - 18:32:17 CEST

This archive was generated by hypermail 2.3.0 : Mon Jun 18 2018 - 01:00:27 CEST