From 84d90343eab2c312acdef4221fb2c54f3920fbc1 Mon Sep 17 00:00:00 2001
From: uz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Date: Sat, 10 Mar 2012 21:46:09 +0000
Subject: [PATCH] Removed several memory leaks.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5595 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 src/sp65/attr.c   | 30 +++++++++++++++++++++++++++++-
 src/sp65/attr.h   |  8 +++++++-
 src/sp65/bitmap.c |  5 +++--
 src/sp65/main.c   |  6 +++---
 src/sp65/pcx.c    |  3 +++
 5 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/src/sp65/attr.c b/src/sp65/attr.c
index 2b8c209fd..8f9b11d74 100644
--- a/src/sp65/attr.c
+++ b/src/sp65/attr.c
@@ -87,6 +87,18 @@ Attr* NewAttr (const char* Name, const char* Value)
 
 
 
+void FreeAttr (Attr* A)
+/* Free an attribute structure */
+{
+    /* Allow NULL pointers */
+    if (A) {
+        xfree (A->Name);
+        xfree (A);
+    }
+}
+
+
+
 void DumpAttrColl (const Collection* C)
 /* Dump a collection of attribute/value pairs for debugging */
 {
@@ -240,7 +252,7 @@ void SplitAddAttr (Collection* C, const char* Combined, const char* Name)
 
         /* Release memory */
         SB_Done (&N);
-    }                                 
+    }
 }
 
 
@@ -296,3 +308,19 @@ Collection* ParseAttrList (const char* List, const char** NameList, unsigned Nam
 
 
 
+void FreeAttrList (Collection* C)
+/* Free a list of attributes */  
+{
+    unsigned I;
+
+    /* Walk over the collection and free all attributes */
+    for (I = 0; I < CollCount (C); ++I) {
+        FreeAttr (CollAtUnchecked (C, I));
+    }
+
+    /* Free the collection itself */
+    FreeCollection (C);
+}
+
+
+
diff --git a/src/sp65/attr.h b/src/sp65/attr.h
index 9379a8020..d7df963d7 100644
--- a/src/sp65/attr.h
+++ b/src/sp65/attr.h
@@ -75,6 +75,9 @@ struct Attr {
 Attr* NewAttr (const char* Name, const char* Value);
 /* Create a new attribute */
 
+void FreeAttr (Attr* A);
+/* Free an attribute structure */
+
 void DumpAttrColl (const Collection* C);
 /* Dump a collection of attribute/value pairs for debugging */
 
@@ -92,7 +95,7 @@ const Attr* GetAttr (const Collection* C, const char* Name);
 
 const Attr* NeedAttr (const Collection* C, const char* Name, const char* Op);
 /* Search for an attribute with the given name and return it. If the attribute
- * is not found, the function terminates with an error using Op as additional 
+ * is not found, the function terminates with an error using Op as additional
  * context in the error message.
  */
 
@@ -123,6 +126,9 @@ Collection* ParseAttrList (const char* List, const char** NameList, unsigned Nam
  * containing Attr entries.
  */
 
+void FreeAttrList (Collection* C);
+/* Free a list of attributes */
+
 
 
 /* End of attr.h */
diff --git a/src/sp65/bitmap.c b/src/sp65/bitmap.c
index 865eab272..b00a01651 100644
--- a/src/sp65/bitmap.c
+++ b/src/sp65/bitmap.c
@@ -81,7 +81,8 @@ void FreeBitmap (Bitmap* B)
 {
     /* Alloc NULL pointers */
     if (B != 0) {
-        /* Free the palette and then the bitmap */
+        /* Free name, palette and then the bitmap */
+        SB_Done (&B->Name);
         xfree (B->Pal);
         xfree(B);
     }
@@ -190,7 +191,7 @@ unsigned GetBitmapColors (const Bitmap* B)
         case bmRGB:
         case bmRGBA:            return (1U << 24);
         default:                Internal ("Unknown bitmap type %u", B->Type);
-    }   
+    }
     /* NOTREACHED */
     return 0;
 }
diff --git a/src/sp65/main.c b/src/sp65/main.c
index c0d912eb0..a6b63ce20 100644
--- a/src/sp65/main.c
+++ b/src/sp65/main.c
@@ -156,7 +156,7 @@ static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg)
     SetOutputData (ConvertTo (C, A));
 
     /* Delete the attribute list */
-    FreeCollection (A);
+    FreeAttrList (A);
 }
 
 
@@ -217,7 +217,7 @@ static void OptRead (const char* Opt attribute ((unused)), const char* Arg)
     B = C = ReadInputFile (A);
 
     /* Delete the attribute list */
-    FreeCollection (A);
+    FreeAttrList (A);
 }
 
 
@@ -286,7 +286,7 @@ static void OptWrite (const char* Opt attribute ((unused)), const char* Arg)
     WriteOutputFile (D, A);
 
     /* Delete the attribute list */
-    FreeCollection (A);
+    FreeAttrList (A);
 }
 
 
diff --git a/src/sp65/pcx.c b/src/sp65/pcx.c
index 53075a1e6..7dc9d9c9b 100644
--- a/src/sp65/pcx.c
+++ b/src/sp65/pcx.c
@@ -437,6 +437,9 @@ Bitmap* ReadPCXFile (const Collection* A)
     /* Close the file */
     fclose (F);
 
+    /* Free memory for the scan line */
+    xfree (L);
+
     /* Free the PCX header */
     FreePCXHeader (P);