Index: /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6144)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6145)
@@ -5,4 +5,6 @@
 #include "pmFPA.h"
 #include "pmChipMosaic.h"
+
+#define MEM_LEAKS 1
 
 // Compare a value with a maximum and minimum
@@ -309,8 +311,13 @@
 
     // Chop off all the component cells, and put in a new one
+    #ifndef MEM_LEAKS
+    pmChipFreeCells(chip);
+    #else
+
     chip->cells = psArrayAlloc(0);
+    #endif
+
     pmCell *cell = pmCellAlloc(chip, NULL, __func__); // New cell
     cellConcepts(cell, cells, xBinChip, yBinChip);
-    psFree(cells);
 
     #if 1
@@ -330,7 +337,7 @@
     // Want the readouts to contain a subimage, but that subimage is the whole image.
     // This preserves the relationship there was before, where freeing the parent frees the child.
-    readout->image = psImageSubset(image, entire);
-    readout->weight = psImageSubset(weight, entire);
-    readout->mask = psImageSubset(mask, entire);
+    readout->image = psMemIncrRefCounter(psImageSubset(image, entire));
+    readout->weight = psMemIncrRefCounter(psImageSubset(weight, entire));
+    readout->mask = psMemIncrRefCounter(psImageSubset(mask, entire));
     psFree(readout);
 
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c	(revision 6144)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c	(revision 6145)
@@ -12,6 +12,6 @@
 * XXX: Should we implement non-linear cell->chip transforms?
 *
-*  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-01-21 06:47:19 $
+*  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-22 03:23:32 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -36,4 +36,5 @@
 {
     if (readout != NULL) {
+        #if 0
         psTrace(__func__, 9, "Removing readout %lx from cell %lx...\n", (size_t) readout, (size_t) readout->parent);
         if (readout->parent) {
@@ -41,19 +42,20 @@
             for (int i = 0; i < readouts->n; i++) {
                 if (readouts->data[i] == readout) {
+                    pmReadout *tmpReadout = readouts->data[i];
                     readouts->data[i] = NULL;
+                    psFree(tmpReadout);
                     break;
                 }
             }
         }
+        #endif
         psTrace(__func__, 9, "Freeing readout %lx\n", (size_t) readout);
+
+        psFree(readout->parent);
+
         psFree(readout->image);
         psFree(readout->mask);
         psFree(readout->weight);
         psFree(readout->analysis);
-        #if 1
-
-        psMemDecrRefCounter(readout->parent);
-        #endif
-
     }
 }
@@ -62,4 +64,5 @@
 {
     if (cell != NULL) {
+        #if 0
         psTrace(__func__, 9, "Removing cell %lx from chip %lx...\n", (size_t)cell, (size_t)cell->parent);
         if (cell->parent) {
@@ -67,10 +70,17 @@
             for (int i = 0; i < cells->n; i++) {
                 if (cells->data[i] == cell) {
+                    pmCell *tmpCell = cells->data[i];
                     cells->data[i] = NULL;
+                    psFree(tmpCell);
                     break;
                 }
             }
         }
+        #endif
         psTrace(__func__, 9, "Freeing cell %lx\n", (size_t)cell);
+
+        pmCellFreeReadouts(cell);
+        psFree(cell->parent);
+
         psFree(cell->toChip);
         psFree(cell->toFPA);
@@ -79,22 +89,4 @@
         psFree(cell->analysis);
         psFree(cell->camera);
-        #if 1
-        //
-        // Set the parent to NULL in all cell->readouts before psFree(cell->readouts)
-        // in order to avoid memory reference counter problems.
-        //
-        psArray *readouts = cell->readouts;
-        for (psS32 i = 0 ; i < readouts->n ; i++) {
-            pmReadout *tmpReadout = readouts->data[i];
-            readouts->data[i] = NULL;
-            tmpReadout->parent = NULL;
-            if (PS_FREE_HIERARCHY == 1) {
-                psTrace(__func__, 9, "Will now free readout %lx...\n", (size_t)tmpReadout);
-                psFree(tmpReadout);
-            }
-        }
-        psMemDecrRefCounter(cell->parent);
-        #endif
-
         psFree(cell->hdu);
     }
@@ -104,4 +96,5 @@
 {
     if (chip != NULL) {
+        #if 0
         psTrace(__func__, 9, "Removing chip %lx from fpa %lx...\n", (size_t)chip, (size_t)chip->parent);
         if (chip->parent) {
@@ -109,10 +102,16 @@
             for (int i = 0; i < chips->n; i++) {
                 if (chips->data[i] == chip) {
+                    pmChip *tmpChip = chips->data[i];
                     chips->data[i] = NULL;
+                    psFree(tmpChip);
                     break;
                 }
             }
         }
+        #endif
         psTrace(__func__, 9, "Freeing chip %lx\n", (size_t)chip);
+
+        pmChipFreeCells(chip);
+        psFree(chip->parent);
 
         psFree(chip->toFPA);
@@ -120,21 +119,4 @@
         psFree(chip->concepts);
         psFree(chip->analysis);
-        #if 1
-        //
-        // Set the parent to NULL in all chip->cells before psFree(chip->cells)
-        // in order to avoid memory reference counter problems.
-        //
-        psArray *cells = chip->cells;
-        for (psS32 i = 0 ; i < cells->n ; i++) {
-            pmCell *tmpCell = cells->data[i];
-            cells->data[i] = NULL;
-            tmpCell->parent = NULL;
-            if (PS_FREE_HIERARCHY == 1) {
-                psFree(tmpCell);
-            }
-        }
-        psMemDecrRefCounter(chip->parent);
-        #endif
-
         psFree(chip->hdu);
     }
@@ -172,4 +154,43 @@
 }
 
+void pmCellFreeReadouts(pmCell *cell)
+{
+    //
+    // Set the parent to NULL in all cell->readouts before psFree(cell->readouts)
+    // in order to avoid memory reference counter problems.
+    //
+    psArray *readouts = cell->readouts;
+    for (psS32 i = 0 ; i < readouts->n ; i++) {
+        pmReadout *tmpReadout = readouts->data[i];
+        readouts->data[i] = NULL;
+        tmpReadout->parent = NULL;
+        psTrace(__func__, 9, "Will now free readout %lx...\n", (size_t)tmpReadout);
+        psFree(tmpReadout); // Drop the readout
+        psFree(cell); // Decrement reference counter on cell, since readout->parent isn't held any more
+    }
+    cell->readouts = psArrayRealloc(cell->readouts, 0);
+}
+
+
+void pmChipFreeCells(pmChip *chip)
+{
+    //
+    // Set the parent to NULL in all chip->cells before psFree(chip->cells)
+    // in order to avoid memory reference counter problems.
+    //
+    psArray *cells = chip->cells;
+    for (int i = 0 ; i < cells->n ; i++) {
+        pmCell *tmpCell = cells->data[i];
+        cells->data[i] = NULL;
+        tmpCell->parent = NULL;
+        pmCellFreeReadouts(tmpCell);// Drop all readouts the cell holds
+        psFree(tmpCell);            // Drop the cell
+        psFree(chip); // Decrement reference counter on chip, since cell->parent isn't held any more
+    }
+    chip->cells = psArrayRealloc(chip->cells, 0);
+}
+
+
+
 void p_pmHDUFree(p_pmHDU *hdu)
 {
@@ -572,8 +593,8 @@
 
         if (! readout->weight) {
-            readout->weight = psImageSubset(weights->data[i], *trimsec);
+            readout->weight = psMemIncrRefCounter(psImageSubset(weights->data[i], *trimsec));
         }
         if (! readout->mask) {
-            readout->mask = psImageSubset(masks->data[i], *trimsec);
+            readout->mask = psMemIncrRefCounter(psImageSubset(masks->data[i], *trimsec));
         }
 
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.h
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.h	(revision 6144)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.h	(revision 6145)
@@ -7,6 +7,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-01-20 09:47:06 $
+*  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-22 03:23:32 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -159,4 +159,6 @@
 pmReadout;
 
+void pmCellFreeReadouts(pmCell *cell);
+void pmChipFreeCells(pmChip *chip);
 
 /** Allocates a pmReadout
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmFPARead.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmFPARead.c	(revision 6144)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmFPARead.c	(revision 6145)
@@ -115,5 +115,5 @@
         pmReadout *readout = pmReadoutAlloc(cell);
 
-        readout->image = psImageSubset(image, *trimsec); // The image corresponding to the trim region
+        readout->image = psMemIncrRefCounter(psImageSubset(image, *trimsec)); // The image corresponding to the trim region
 
         // Get the list of overscans
