Index: /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6350)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6351)
@@ -157,6 +157,4 @@
         }
 
-        psMetadataPrint(cell->concepts, 10);
-
         nCells++;
         gain       += psMetadataLookupF32(NULL, cell->concepts, "CELL.GAIN");
@@ -303,7 +301,7 @@
         psArrayElementsFree(hdu->weights);
         psArrayElementsFree(hdu->masks);
-        hdu->images  = psArrayAlloc(1);
-        hdu->weights = psArrayAlloc(1);
-        hdu->masks   = psArrayAlloc(1);
+        hdu->images  = psArrayRealloc(hdu->images,1);
+        hdu->weights = psArrayRealloc(hdu->weights, 1);
+        hdu->masks   = psArrayRealloc(hdu->masks, 1);
         hdu->images->data[0]  = image;
         hdu->weights->data[0] = weight;
@@ -314,32 +312,24 @@
 
     // Chop off all the component cells, and put in a new one
-    #if !MEM_LEAKS
+    pmCell *newCell = pmCellAlloc(NULL, NULL, __func__); // New cell
+    cellConcepts(newCell, cells, xBinChip, yBinChip);
     pmChipFreeCells(chip);
-    #else
-
-    chip->cells = psArrayAlloc(0);
-    #endif
-
-    pmCell *cell = pmCellAlloc(chip, NULL, __func__); // New cell
-    cellConcepts(cell, cells, xBinChip, yBinChip);
-
-    #if 0
-    // XXX For the sake of getting something working, I am not going to bother with sorting out where
-    // the double free is coming from.  I'm going to drop the pointers on the array and create a memory
-    // leak.  We can clean this up later, when we're not under as much pressure.
-    psArrayElementsFree(chip->cells);
-    chip->cells->n = 0;
-    #endif
-
-    cell->exists = true;
-    cell->process = true;
-    pmReadout *readout = pmReadoutAlloc(cell); // New readout
-    psRegion entire = {0.0, 0.0, 0.0, 0.0};
+    // Have to put in the new cell manually, since we didn't want to put it in before blowing the cells away.
+    newCell->parent = chip;
+    psArrayAdd(chip->cells, 1, newCell);
+    newCell->exists = true;
+    newCell->process = true;
+
+    // Now make a new readout to go in the new cell
+    pmReadout *newReadout = pmReadoutAlloc(newCell); // New readout
     // 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);
-    psFree(readout);
+    psRegion entire = {0.0, 0.0, 0.0, 0.0};
+    newReadout->image = psImageSubset(image, entire);
+    newReadout->weight = psImageSubset(weight, entire);
+    newReadout->mask = psImageSubset(mask, entire);
+    // Drop references
+    psFree(newReadout);
+    psFree(newCell);
 
     // Well, we've stuffed around with the camera configuration, so it's no longer valid...
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmConcepts.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmConcepts.c	(revision 6350)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmConcepts.c	(revision 6351)
@@ -85,7 +85,10 @@
 {
     switch (blank->type) {
-    case PS_DATA_STRING:
-        return psMetadataItemAllocStr(blank->name, blank->comment,
-                                      pmConceptReadString(fpa, chip, cell, db, blank->name));
+    case PS_DATA_STRING: {
+            psString string = pmConceptReadString(fpa, chip, cell, db, blank->name);
+            psMetadataItem *item = psMetadataItemAllocStr(blank->name, blank->comment, string);
+            psFree(string);
+            return item;
+        }
     case PS_DATA_S32:
         return psMetadataItemAllocS32(blank->name, blank->comment,
@@ -152,4 +155,5 @@
         }
         psMetadataAddItem(target, conceptItem, PS_LIST_TAIL, PS_META_REPLACE);
+        psFree(conceptItem);            // Drop reference
     }
     psFree(specsIter);
Index: /branches/eam_rel9_p0/psModules/src/imsubtract/pmSubtractBias.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/imsubtract/pmSubtractBias.c	(revision 6350)
+++ /branches/eam_rel9_p0/psModules/src/imsubtract/pmSubtractBias.c	(revision 6351)
@@ -11,6 +11,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.6.8.1.2.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-01-20 09:47:06 $
+ *  @version $Revision: 1.6.8.1.2.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-02-08 00:52:13 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -376,14 +376,20 @@
         break;
     case PM_FIT_POLY_ORD:
-        overscanOpts->poly = psPolynomial1DAlloc(overscanOpts->order, PS_POLYNOMIAL_ORD);
-        overscanOpts->poly = psVectorFitPolynomial1D(overscanOpts->poly, mask, 1, reduced, NULL,
-                             ordinate);
+        if (overscanOpts->poly && (overscanOpts->poly->nX != overscanOpts->order ||
+                                   overscanOpts->poly->type != PS_POLYNOMIAL_ORD)) {
+            psFree(overscanOpts->poly);
+            overscanOpts->poly = psPolynomial1DAlloc(overscanOpts->order, PS_POLYNOMIAL_ORD);
+        }
+        psVectorFitPolynomial1D(overscanOpts->poly, mask, 1, reduced, NULL, ordinate);
         psFree(reduced);
         reduced = psPolynomial1DEvalVector(overscanOpts->poly, ordinate);
         break;
     case PM_FIT_POLY_CHEBY:
-        overscanOpts->poly = psPolynomial1DAlloc(overscanOpts->order, PS_POLYNOMIAL_CHEB);
-        overscanOpts->poly = psVectorFitPolynomial1D(overscanOpts->poly, mask, 1, reduced, NULL,
-                             ordinate);
+        if (overscanOpts->poly && (overscanOpts->poly->nX != overscanOpts->order ||
+                                   overscanOpts->poly->type != PS_POLYNOMIAL_CHEB)) {
+            psFree(overscanOpts->poly);
+            overscanOpts->poly = psPolynomial1DAlloc(overscanOpts->order, PS_POLYNOMIAL_CHEB);
+        }
+        psVectorFitPolynomial1D(overscanOpts->poly, mask, 1, reduced, NULL, ordinate);
         psFree(reduced);
         reduced = psPolynomial1DEvalVector(overscanOpts->poly, ordinate);
@@ -501,4 +507,5 @@
                 // Reduce the overscans
                 psVector *reduced = overscanVector(overscanOpts, pixels, myStats);
+                psFree(pixels);
                 if (! reduced) {
                     return in;
@@ -544,4 +551,5 @@
                 // Reduce the overscans
                 psVector *reduced = overscanVector(overscanOpts, pixels, myStats);
+                psFree(pixels);
                 if (! reduced) {
                     return in;
