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
     1.1 --- a/cline.c	Fri May 27 12:49:33 2011 +0200
     1.2 +++ b/cline.c	Fri May 27 13:20:15 2011 +0200
     1.3 @@ -3,15 +3,17 @@
     1.4  
     1.5  settings_t* new_settings_t() {
     1.6    settings_t *settings = malloc(sizeof(settings_t*));
     1.7 -#ifdef _WIN32
     1.8 -  settings->fileSeparator      = '\\';
     1.9 -#else
    1.10 -  settings->fileSeparator      = '/';
    1.11 -#endif /* _WIN32 */
    1.12 -  settings->suffixc            = 1;
    1.13 -  settings->recursive          = false;
    1.14 -  settings->includeSuffixes    = false;
    1.15 -  settings->matchesOnly        = false;
    1.16 +  if (settings != NULL) {
    1.17 +  #ifdef _WIN32
    1.18 +    settings->fileSeparator      = '\\';
    1.19 +  #else
    1.20 +    settings->fileSeparator      = '/';
    1.21 +  #endif /* _WIN32 */
    1.22 +    settings->suffixc            = 1;
    1.23 +    settings->recursive          = false;
    1.24 +    settings->includeSuffixes    = false;
    1.25 +    settings->matchesOnly        = false;
    1.26 +  }
    1.27    
    1.28    return settings;
    1.29  }
    1.30 @@ -53,6 +55,10 @@
    1.31  
    1.32    // Settings
    1.33    settings_t *settings = new_settings_t();
    1.34 +  if (settings == NULL) {
    1.35 +    fprintf(stderr, "Memory allocation failed.\n");
    1.36 +    return 1;
    1.37 +  }
    1.38  
    1.39    // Program name
    1.40    char* prgName = strrchr(argv[0], settings->fileSeparator);
    1.41 @@ -84,13 +90,15 @@
    1.42      if ((argflags & 2) > 0) {
    1.43        if ((checked & 1) > 0) {
    1.44          printHelpText(prgName);
    1.45 -        return -1;
    1.46 +        destroy_settings_t(settings);
    1.47 +        return 1;
    1.48        }
    1.49        settings->includeSuffixes = true;
    1.50        t++;
    1.51        if (t >= argc) {
    1.52          printHelpText(prgName);
    1.53 -        return -1;
    1.54 +        destroy_settings_t(settings);
    1.55 +        return 1;
    1.56        }
    1.57        suffix = argv[t]; 
    1.58        checked |= 1;
    1.59 @@ -99,13 +107,15 @@
    1.60      if ((argflags & 4) > 0) {
    1.61        if ((checked & 1) > 0) {
    1.62          printHelpText(prgName);
    1.63 -        return -1;
    1.64 +        destroy_settings_t(settings);
    1.65 +        return 1;
    1.66        }
    1.67        settings->includeSuffixes = false;
    1.68        t++;
    1.69        if (t >= argc) {
    1.70          printHelpText(prgName);
    1.71 -        return -1;
    1.72 +        destroy_settings_t(settings);
    1.73 +        return 1;
    1.74        }
    1.75        suffix = argv[t];
    1.76        checked |= 1;
    1.77 @@ -114,7 +124,8 @@
    1.78      if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
    1.79        if ((checked & 2) > 0) {
    1.80          printHelpText(prgName);
    1.81 -        return -1;
    1.82 +        destroy_settings_t(settings);
    1.83 +        return 1;
    1.84        }
    1.85        checked |= 2;
    1.86        showHelp = true;
    1.87 @@ -123,7 +134,8 @@
    1.88      if ((argflags & 24) > 0) {
    1.89        if ((checked & 4) > 0) {
    1.90          printHelpText(prgName);
    1.91 -        return -1;
    1.92 +        destroy_settings_t(settings);
    1.93 +        return 1;
    1.94        }
    1.95        checked |= 4;
    1.96        settings->recursive = true;
    1.97 @@ -131,7 +143,8 @@
    1.98      if ((argflags & 32) > 0) {
    1.99        if ((checked & 32) > 0) {
   1.100          printHelpText(prgName);
   1.101 -        return -1;
   1.102 +        destroy_settings_t(settings);
   1.103 +        return 1;
   1.104        }
   1.105        checked |= 32;
   1.106        settings->matchesOnly = true;
   1.107 @@ -140,7 +153,8 @@
   1.108      if (argflags == 0) {
   1.109        if ((checked & 8) > 0) {
   1.110          printHelpText(prgName);
   1.111 -        return -1;
   1.112 +        destroy_settings_t(settings);
   1.113 +        return 1;
   1.114        }
   1.115        checked |= 8;
   1.116        directory = argv[t];
   1.117 @@ -150,6 +164,7 @@
   1.118    // Show help and quit
   1.119    if (showHelp) {
   1.120      printHelpText(prgName);
   1.121 +    destroy_settings_t(settings);
   1.122      return 0;
   1.123    }
   1.124  
   1.125 @@ -172,6 +187,7 @@
   1.126    settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
   1.127    if (settings->suffixv == NULL) {
   1.128      fprintf(stderr, "Memory allocation failed.\n");
   1.129 +    destroy_settings_t(settings);
   1.130      return 1;
   1.131    }
   1.132    finder = strtok(suffix, ",");

mercurial