changeset 2209:e37dd763d329

Plug some memory leaks.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 14 Oct 2019 10:02:38 +0300
parents 784ff0c54a7d
children 2f54bc8411c1
files stitchmap.c
diffstat 1 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/stitchmap.c	Mon Oct 14 09:58:02 2019 +0300
+++ b/stitchmap.c	Mon Oct 14 10:02:38 2019 +0300
@@ -410,7 +410,7 @@
 int main(int argc, char *argv[])
 {
     BOOL isOK;
-    int i, currRounds, nmapBlocks, currBlocks,
+    int i, currRounds, nmapBlocks = 0, currBlocks,
         worldX0, worldY0, worldX1, worldY1,
         offsetX = 0, offsetY = 0,
         res = 0;
@@ -459,7 +459,7 @@
      */
     for (nmapBlocks = i = 0; i < nsrcFiles; i++)
     {
-        FILE *tmpFile;
+        FILE *tmpFile = NULL;
         char centerCh;
 
         if (optCenterCh > 0)
@@ -498,11 +498,12 @@
                 {
                     nmapBlocks++;
                     mapBlocks = (MapBlock **) th_realloc(mapBlocks, sizeof(MapBlock *) * nmapBlocks);
-                    if (!mapBlocks)
+                    if (mapBlocks == NULL)
                     {
                         fclose(tmpFile);
                         THERR("Could not allocate/extend mapblock pointer structure (#%d)\n", nmapBlocks);
-                        exit(3);
+                        res = 18;
+                        goto exit;
                     }
 
                     mapBlockClean(tmp, (unsigned char *) optCleanChars, strlen(optCleanChars));
@@ -640,7 +641,8 @@
                     {
                         THERR("mapBlockPut(%d, %d, %d) failed!\n",
                             offsetX, offsetY, i);
-                        exit(9);
+                        res = 9;
+                        goto exit;
                     }
                     tmp->mark = TRUE;
 
@@ -686,7 +688,8 @@
         {
             THERR("Error opening output file '%s'!\n",
                 destFile);
-            exit(1);
+            res = 1;
+            goto exit;
         }
 
         mapBlockPrint(tmpFile, worldMap);
@@ -719,5 +722,13 @@
     }
 
 exit:
+    mapBlockFree(worldMap);
+    mapBlockFree(initialMap);
+
+    for (i = 0; i < nmapBlocks; i++)
+        mapBlockFree(mapBlocks[i]);
+
+    th_free(mapBlocks);
+
     return res;
 }