Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c	(revision 33459)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c	(revision 33460)
@@ -26,4 +26,8 @@
   FTable ftable;
 
+  header.buffer = NULL;
+  matrix.buffer = NULL;
+  ftable.buffer = NULL;
+  theader.buffer = NULL;
   BrightCatalog *catalog = NULL;
 
@@ -31,5 +35,5 @@
   if (!f) {
     fprintf (stderr, "ERROR: cannot open image subset file %s\n", filename);
-    return NULL;
+    goto escape;
   }
 
@@ -37,12 +41,9 @@
   if (!gfits_fread_header (f, &header)) {
     if (VERBOSE) fprintf (stderr, "can't read image subset header\n");
-    fclose (f);
-    return NULL;
+    goto escape;
   }
   if (!gfits_fread_matrix (f, &matrix, &header)) {
     if (VERBOSE) fprintf (stderr, "can't read image subset matrix\n");
-    gfits_free_header (&header);
-    fclose (f);
-    return NULL;
+    goto escape;
   }
 
@@ -56,8 +57,8 @@
   { 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
  
     // need to create and assign to flat-field correction
@@ -78,5 +79,6 @@
     GET_COLUMN(catID,     "CAT_ID",     int);
     GET_COLUMN(photcode,  "PHOTCODE",   short);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     MeasureTiny *measure = NULL;
@@ -127,8 +129,8 @@
 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
  
     // need to create and assign to flat-field correction
@@ -139,5 +141,6 @@
     GET_COLUMN(flags,         "FLAGS",       int);
     GET_COLUMN(catID,         "CAT_ID",      int);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     AverageTiny *average = NULL;
@@ -168,8 +171,8 @@
 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
  
     // need to create and assign to flat-field correction
@@ -182,5 +185,6 @@
     GET_COLUMN(M_20,  "MAG_20",   short);
     GET_COLUMN(M_80,  "MAG_80",   short);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     SecFilt *secfilt = NULL;
@@ -210,7 +214,21 @@
   }
 
-  // close files
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  if (catalog) free (catalog);
+
+  fclose (f);
 
   return catalog;
+
+escape:
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  gfits_free_header (&theader);
+  gfits_free_table  (&ftable);
+  if (catalog) free (catalog);
+
+  fclose (f);
+  return NULL;
 }
 
