Plugging some memory leaks


To ng-spice-devel <ng-spice-devel@ieee.ing.uniroma1.it>
From "Arno W. Peters" <a.w.peters@ieee.org>
Date Fri, 31 Mar 2000 22:46:37 +0200
Delivered-To mailing list ng-spice-devel@ieee.ing.uniroma1.it
Mailing-List contact ng-spice-devel-help@ieee.ing.uniroma1.it; run by ezmlm
Reply-To ng-spice-devel@ieee.ing.uniroma1.it
User-Agent Mutt/1.1.9i


I have been busy searching out memory leaks by using the debauch tool
(http://quorum.tamu.edu/jon/gnu/).  I had to rewrite the script a
little in perl to get it to work together with my gdb.  I have it
available on request.  I will send the output of the tool in another
email.

This a modest beginning.  Try it out.  See if it works for you.

Greetings,
-- 
Arno

diff -ruN ng-spice/src/main.c new-ng-spice/src/main.c
--- ng-spice/src/main.c Sun Mar 26 12:30:42 2000
+++ new-ng-spice/src/main.c     Fri Mar 31 22:17:19 2000
@@ -6,6 +6,11 @@
 /*
  * The main routine for ngspice
  */
+#include <stdio.h>
+#include <setjmp.h>
+#include <signal.h>
+
+#include <sys/types.h>
 
 
 #include "ngspice.h"
@@ -17,17 +22,11 @@
 #include "ftedev.h"
 #include "ftedebug.h"
 #include "const.h"
-#include <setjmp.h>
-#include <signal.h>
-
-#include <sys/types.h>
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 
-
-
 #ifndef HAVE_GETRUSAGE
 #ifdef HAVE_FTIME
 #include <sys/timeb.h>
@@ -152,9 +151,6 @@
 #endif /* SIMULATOR */
 
 
-#ifdef HAVE_PWD_H
-    struct passwd *pw;
-#endif
     char buf[BSIZE_SP];
     bool readinit = TRUE, rflag = FALSE, ciprefix();
     bool istty = TRUE, iflag = FALSE, qflag = FALSE;
@@ -418,11 +414,16 @@
         if (access(".spiceinit", 0) == 0)
             inp_source(".spiceinit");
         else {
+           char *s;
+           struct passwd *pw;
+
             pw = getpwuid(getuid());
-            (void) strcpy(buf, pw->pw_dir);
-            (void) strcat(buf, "/.spiceinit");
-            if (access(buf, 0) == 0)
-                inp_source(buf);
+           asprintf(&s, "%s/.spiceinit", pw->pw_dir);
+            if (access(s, 0) == 0)
+                inp_source(s);
+           free(s);
+           /* XXX Need to free() char* fields in pw as well? XXX */
+           free(pw);
         }
     }
 #  else /* ~ HAVE_PWD_H */
@@ -601,6 +602,7 @@
 int
 shutdown(int exitval)
 {
+    cleanvars();
 #ifdef PARALLEL_ARCH
     if (exitval == EXIT_BAD) {
        Error("Fatal error in SPICE", -1);
diff -ruN ng-spice/src/misc/ivars.c new-ng-spice/src/misc/ivars.c
--- ng-spice/src/misc/ivars.c   Sun Mar 26 12:30:52 2000
+++ new-ng-spice/src/misc/ivars.c       Fri Mar 31 21:26:39 2000
@@ -6,32 +6,32 @@
 #include "ivars.h"
 #include <stdio.h>
 
-char   *Spice_Path;
-char   *News_File;
-char   *Default_MFB_Cap;
-char   *Help_Path;
-char   *Lib_Path;
+char *Spice_Path;
+char *News_File;
+char *Default_MFB_Cap;
+char *Help_Path;
+char *Lib_Path;
 
 
 static void
 env_overr(char **v, char *e)
 {
-    char    *p;
+    char *p;
     if (v && e && (p = getenv(e)))
        *v = p;
 }
 
 static void
-mkvar(char **p, char *b, char *v, char *e)
+mkvar(char **p, char *path_prefix, char *var_dir, char *env_var)
 {
-    char buffer[256];
+    char *buffer;
 
-    env_overr(p, e);
-    if (!*p) {
-       sprintf(buffer, "%s%s%s", b, DIR_PATHSEP, v);
-       *p = tmalloc(strlen(buffer) + 1);
-       strcpy(*p, buffer);
-    }
+    /* Override by environment variables */
+    buffer = getenv(env_var);
+    if (buffer)
+       asprintf(p, "%s", buffer);
+    else
+       asprintf(p, "%s%s%s", path_prefix, DIR_PATHSEP, var_dir);
 }
 
 void
@@ -51,5 +51,14 @@
     env_overr(&Bug_Addr, "SPICE_BUGADDR");
     env_overr(&Def_Editor, "SPICE_EDITOR");
     env_overr(&AsciiRawFile, "SPICE_ASCIIRAWFILE");
+}
 
+void
+cleanvars(void)
+{
+    free(News_File);
+    free(Default_MFB_Cap);
+    free(Help_Path);
+    free(Lib_Path);
+    free(Spice_Path);
 }
diff -ruN ng-spice/src/misc/ivars.h new-ng-spice/src/misc/ivars.h
--- ng-spice/src/misc/ivars.h   Sun Mar 26 12:30:52 2000
+++ new-ng-spice/src/misc/ivars.h       Fri Mar 31 20:33:34 2000
@@ -8,5 +8,6 @@
 
 
 void ivars(void);
+void cleanvars(void);
 
 #endif
diff -ruN ng-spice/src/parser/complete.c new-ng-spice/src/parser/complete.c
--- ng-spice/src/parser/complete.c      Sun Mar 26 12:30:52 2000
+++ new-ng-spice/src/parser/complete.c  Fri Mar 31 19:40:18 2000
@@ -680,6 +680,11 @@
 cdelete(struct ccom *node)
 {
     node->cc_invalid = 1;
+    free(node->cc_name);
+    free(node->cc_child);
+    free(node->cc_sibling);
+    free(node->cc_ysibling);
+    free(node->cc_parent);
     return;
 }
 

PGP signature


Partial thread listing: