Fixed memory leak when exiting the programm ahead of time

Fri, 27 May 2011 13:20:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2011 13:20:15 +0200
changeset 5
9393eff3d2f9
parent 4
c3acfb3b4957
child 6
be923400164c

Fixed memory leak when exiting the programm ahead of time

cline.c file | annotate | diff | comparison | revisions
--- a/cline.c	Fri May 27 12:49:33 2011 +0200
+++ b/cline.c	Fri May 27 13:20:15 2011 +0200
@@ -3,15 +3,17 @@
 
 settings_t* new_settings_t() {
   settings_t *settings = malloc(sizeof(settings_t*));
-#ifdef _WIN32
-  settings->fileSeparator      = '\\';
-#else
-  settings->fileSeparator      = '/';
-#endif /* _WIN32 */
-  settings->suffixc            = 1;
-  settings->recursive          = false;
-  settings->includeSuffixes    = false;
-  settings->matchesOnly        = false;
+  if (settings != NULL) {
+  #ifdef _WIN32
+    settings->fileSeparator      = '\\';
+  #else
+    settings->fileSeparator      = '/';
+  #endif /* _WIN32 */
+    settings->suffixc            = 1;
+    settings->recursive          = false;
+    settings->includeSuffixes    = false;
+    settings->matchesOnly        = false;
+  }
   
   return settings;
 }
@@ -53,6 +55,10 @@
 
   // Settings
   settings_t *settings = new_settings_t();
+  if (settings == NULL) {
+    fprintf(stderr, "Memory allocation failed.\n");
+    return 1;
+  }
 
   // Program name
   char* prgName = strrchr(argv[0], settings->fileSeparator);
@@ -84,13 +90,15 @@
     if ((argflags & 2) > 0) {
       if ((checked & 1) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       settings->includeSuffixes = true;
       t++;
       if (t >= argc) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       suffix = argv[t]; 
       checked |= 1;
@@ -99,13 +107,15 @@
     if ((argflags & 4) > 0) {
       if ((checked & 1) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       settings->includeSuffixes = false;
       t++;
       if (t >= argc) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       suffix = argv[t];
       checked |= 1;
@@ -114,7 +124,8 @@
     if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
       if ((checked & 2) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       checked |= 2;
       showHelp = true;
@@ -123,7 +134,8 @@
     if ((argflags & 24) > 0) {
       if ((checked & 4) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       checked |= 4;
       settings->recursive = true;
@@ -131,7 +143,8 @@
     if ((argflags & 32) > 0) {
       if ((checked & 32) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       checked |= 32;
       settings->matchesOnly = true;
@@ -140,7 +153,8 @@
     if (argflags == 0) {
       if ((checked & 8) > 0) {
         printHelpText(prgName);
-        return -1;
+        destroy_settings_t(settings);
+        return 1;
       }
       checked |= 8;
       directory = argv[t];
@@ -150,6 +164,7 @@
   // Show help and quit
   if (showHelp) {
     printHelpText(prgName);
+    destroy_settings_t(settings);
     return 0;
   }
 
@@ -172,6 +187,7 @@
   settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
   if (settings->suffixv == NULL) {
     fprintf(stderr, "Memory allocation failed.\n");
+    destroy_settings_t(settings);
     return 1;
   }
   finder = strtok(suffix, ",");

mercurial