Index: trunk/psModules/src/pmAstrometry.c
===================================================================
--- trunk/psModules/src/pmAstrometry.c	(revision 4997)
+++ trunk/psModules/src/pmAstrometry.c	(revision 5052)
@@ -8,6 +8,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-09-12 20:38:25 $
+*  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-09-14 19:21:41 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -48,5 +48,7 @@
 XXX: Is this the correct way to free database structs?
  
-XXX: The meory dereferencing is not quite right with these functions.  If I
+ 
+ 
+XXX: The memory dereferencing is not quite right with these functions.  If I
 call the alloc functions with non-NULL pointers, there will be memory leaks.
  *****************************************************************************/
@@ -60,4 +62,12 @@
         psFree(fpa->analysis);
         psFree(fpa->camera);
+        //
+        // Set the parent to NULL in all fpa->chips before psFree(fpa->chips)
+        // in order to avoid memory reference counter problems.
+        //
+        for (psS32 i = 0 ; i < fpa->chips->n ; i++) {
+            pmChip *tmpChip = (pmChip *) fpa->chips->data[i];
+            tmpChip->parent = NULL;
+        }
         psFree(fpa->chips);
         psFree(fpa->header);
@@ -73,4 +83,12 @@
         psFree(chip->concepts);
         psFree(chip->analysis);
+        //
+        // Set the parent to NULL in all chip->cells before psFree(chip->cells)
+        // in order to avoid memory reference counter problems.
+        //
+        for (psS32 i = 0 ; i < chip->cells->n ; i++) {
+            pmCell *tmpCell = (pmCell *) chip->cells->data[i];
+            tmpCell->parent = NULL;
+        }
         psFree(chip->cells);
         psFree(chip->parent);
@@ -89,4 +107,12 @@
         psFree(cell->concepts);
         psFree(cell->analysis);
+        //
+        // Set the parent to NULL in all cell->readouts before psFree(cell->readouts)
+        // in order to avoid memory reference counter problems.
+        //
+        for (psS32 i = 0 ; i < cell->readouts->n ; i++) {
+            pmReadout *tmpReadout = (pmReadout *) cell->readouts->data[i];
+            tmpReadout->parent = NULL;
+        }
         psFree(cell->readouts);
         psFree(cell->parent);
@@ -122,5 +148,4 @@
     tmpReadout->concepts = psMetadataAlloc();
     tmpReadout->parent = cell;
-    psMemIncrRefCounter((psPtr) cell);
     if (cell != NULL) {
         cell->readouts = psArrayAdd(cell->readouts, 1, (psPtr) tmpReadout);
@@ -143,5 +168,4 @@
     tmpCell->readouts = psArrayAlloc(0);
     tmpCell->parent = chip;
-    psMemIncrRefCounter((psPtr) chip);
     if (chip != NULL) {
         chip->cells = psArrayAdd(chip->cells, 1, (psPtr) tmpCell);
@@ -168,8 +192,6 @@
     tmpChip->cells = psArrayAlloc(0);
     tmpChip->parent = fpa;
-    psMemIncrRefCounter((psPtr) fpa);
     if (fpa != NULL) {
         fpa->chips = psArrayAdd(fpa->chips, 1, (psPtr) tmpChip);
-        psMemIncrRefCounter((psPtr) tmpChip);
     }
     tmpChip->valid = false;
