[PATCH xset] Resource cleanup fixes

Jeff Smith whydoubt at yahoo.com
Sat Mar 20 19:41:45 PDT 2010


Running xset through valgrind revealed numerous places where resources were
not being cleaned up properly.

Signed-off-by: Jeff Smith <whydoubt at yahoo.com>
---
 xset.c |   33 +++++++++++++++++++++++++++++----
 1 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/xset.c b/xset.c
index 7458ee9..d03ed51 100644
--- a/xset.c
+++ b/xset.c
@@ -145,6 +145,8 @@ static char *progName;
 
 static int error_status = 0;
 
+static Display *display = NULL;
+
 static int is_number(char *arg, int maximum);
 static void set_click(Display *dpy, int percent);
 static void set_bell_vol(Display *dpy, int percent);
@@ -229,6 +231,8 @@ main(int argc, char *argv[])
 		argv[0], XDisplayName(disp));
 	exit(EXIT_FAILURE);
     }
+    display = dpy;
+
     XSetErrorHandler(local_xerror);
     for (i = 1; i < argc;) {
 	arg = argv[i++];
@@ -560,6 +564,7 @@ main(int argc, char *argv[])
 			fprintf(stderr,
 			    "  standby time of %d is greater than suspend time of %d\n",
 			    standby_timeout, suspend_timeout);
+			XCloseDisplay(dpy);
 			exit(EXIT_FAILURE);
 		    }
 		    if ((off_timeout != 0) && (suspend_timeout > off_timeout)) {
@@ -567,6 +572,7 @@ main(int argc, char *argv[])
 			fprintf(stderr,
 			    "  suspend time of %d is greater than off time of %d\n",
 			    suspend_timeout, off_timeout);
+			XCloseDisplay(dpy);
 			exit(EXIT_FAILURE);
 		    }
 		    if ((suspend_timeout == 0) && (off_timeout != 0) &&
@@ -575,6 +581,7 @@ main(int argc, char *argv[])
 			fprintf(stderr,
 			    "  standby time of %d is greater than off time of %d\n",
 			    standby_timeout, off_timeout);
+			XCloseDisplay(dpy);
 			exit(EXIT_FAILURE);
 		    }
 		    DPMSEnable(dpy);
@@ -1340,7 +1347,7 @@ query(Display *dpy)
     XF86MiscKbdSettings kbdinfo;
 #endif
 #ifdef XKB
-    XkbDescPtr xkb;
+    XkbDescPtr xkb = NULL;
     int xkbmajor = XkbMajorVersion, xkbminor = XkbMinorVersion;
     int xkbopcode, xkbevent, xkberror;
 #endif
@@ -1352,7 +1359,6 @@ query(Display *dpy)
     XGetKeyboardControl(dpy, &values);
     XGetPointerControl(dpy, &acc_num, &acc_denom, &threshold);
     XGetScreenSaver(dpy, &timeout, &interval, &prefer_blank, &allow_exp);
-    font_path = XGetFontPath(dpy, &npaths);
 
     printf("Keyboard Control:\n");
     printf
@@ -1450,6 +1456,10 @@ query(Display *dpy)
 	printf("  auto repeat delay:  %d    repeat rate:  %d\n",
 	       kbdinfo.delay, kbdinfo.rate);
 #endif
+#ifdef XKB
+    if (xkb != NULL)
+	XkbFreeKeyboard(xkb, 0, True);
+#endif
     printf("  auto repeating keys:  ");
     for (i = 0; i < 4; i++) {
 	if (i)
@@ -1481,14 +1491,17 @@ query(Display *dpy)
 								       scr));
 
     printf("Font Path:\n");
+    font_path = XGetFontPath(dpy, &npaths);
     if (npaths) {
-	printf("  %s", *font_path++);
+	char **fp = font_path;
+	printf("  %s", *fp++);
 	for (--npaths; npaths; npaths--)
-	    printf(",%s", *font_path++);
+	    printf(",%s", *fp++);
 	printf("\n");
     } else {
 	printf("  (empty)\n");
     }
+    XFreeFontPath(font_path);
 
 #ifdef MITMISC
     {
@@ -1578,6 +1591,13 @@ query(Display *dpy)
 	    printf("  Config file:  %s\n", paths.configfile);
 	    printf("  Modules path: %s\n", paths.modulepath);
 	    printf("  Log file:     %s\n", paths.logfile);
+	    /* There is no funtion to complement XF86MiscGetFilePaths */ 
+	    if (paths.configfile)
+		XFree(paths.configfile);
+	    if (paths.modulepath)
+		XFree(paths.modulepath);
+	    if (paths.logfile)
+		XFree(paths.logfile);
 	}
     }
 #endif
@@ -1720,6 +1740,9 @@ usage(char *fmt, ...)
     fprintf(stderr, "\t s expose             s noexpose\n");
     fprintf(stderr, "\t s activate           s reset\n");
     fprintf(stderr, "    For status information:  q\n");
+
+    if (display != NULL)
+        XCloseDisplay(display);
     exit(EXIT_SUCCESS);
 }
 
@@ -1727,6 +1750,8 @@ static void
 error(char *message)
 {
     fprintf(stderr, "%s: %s\n", progName, message);
+    if (display != NULL)
+        XCloseDisplay(display);
     exit(EXIT_FAILURE);
 }
 
-- 
1.6.0.6

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the xorg-devel mailing list