Index: /trunk/psModules/src/pmAstrometry.c
===================================================================
--- /trunk/psModules/src/pmAstrometry.c	(revision 5051)
+++ /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;
Index: /trunk/psModules/test/tst_pmAstrometry.c
===================================================================
--- /trunk/psModules/test/tst_pmAstrometry.c	(revision 5051)
+++ /trunk/psModules/test/tst_pmAstrometry.c	(revision 5052)
@@ -5,6 +5,6 @@
  *  @author George Gusciora, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-13 20:54:04 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-14 19:21:41 $
  *
  *  XXX: The meory dereferencing is not quite right with these functions.
@@ -110,14 +110,12 @@
 {
     // XXX: Do something more with these arguments.
-    /*
-        const psMetadata *camera = psMetadataAlloc();
-        psDB *db = NULL;
-        pmFPA* fpa = pmFPAAlloc(camera, db);
-        if (fpa == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.");
-            return 1;
-        }
-    */
-    pmFPA* fpa = NULL;
+    const psMetadata *camera = psMetadataAlloc();
+    psDB *db = NULL;
+    pmFPA* fpa = pmFPAAlloc(camera, db);
+    if (fpa == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.");
+        return 1;
+    }
+
     pmChip *chip = pmChipAlloc(fpa);
     if (chip == NULL) {
@@ -186,32 +184,28 @@
     }
 
-    //    psFree(camera);
+    psFree(fpa);
     psFree(chip);
-    psFree(fpa);
+    psFree(camera);
 
     return 0;
 }
 
-
 static psS32 testCellAlloc(void)
 {
     // XXX: Do something more with these arguments.
-    /*
-        const psMetadata *camera = psMetadataAlloc();
-        psDB *db = NULL;
-        pmFPA* fpa = pmFPAAlloc(camera, db);
-        if (fpa == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.n");
-            return 1;
-        }
-     
-        pmChip *chip = pmChipAlloc(fpa);
-        if (chip == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.n");
-            return 2;
-        }
-    */
-    pmFPA* fpa = NULL;
-    pmChip *chip = NULL;
+    const psMetadata *camera = psMetadataAlloc();
+    psDB *db = NULL;
+    pmFPA* fpa = pmFPAAlloc(camera, db);
+    if (fpa == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.n");
+        return 1;
+    }
+
+    pmChip *chip = pmChipAlloc(fpa);
+    if (chip == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.n");
+        return 2;
+    }
+
     pmCell *cell = pmCellAlloc(chip);
     if (cell == NULL) {
@@ -288,4 +282,5 @@
     psFree(chip);
     psFree(cell);
+    psFree(camera);
 
     return 0;
@@ -295,30 +290,25 @@
 {
     // XXX: Do something more with these arguments.
-    /*
-        const psMetadata *camera = psMetadataAlloc();
-        psDB *db = NULL;
-        pmFPA* fpa = pmFPAAlloc(camera, db);
-     
-        if (fpa == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.\n");
-            return 1;
-        }
-     
-        pmChip *chip = pmChipAlloc(fpa);
-        if (chip == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.\n");
-            return 2;
-        }
-     
-        pmCell *cell = pmCellAlloc(chip);
-        if (cell == NULL) {
-            psLogMsg(__func__,PS_LOG_ERROR, "pmCellAlloc returned a NULL.\n");
-            return 3;
-        }
-    */
-
-    pmFPA* fpa = NULL;
-    pmChip *chip = NULL;
-    pmCell *cell = NULL;
+    const psMetadata *camera = psMetadataAlloc();
+    psDB *db = NULL;
+    pmFPA* fpa = pmFPAAlloc(camera, db);
+
+    if (fpa == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.\n");
+        return 1;
+    }
+
+    pmChip *chip = pmChipAlloc(fpa);
+    if (chip == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.\n");
+        return 2;
+    }
+
+    pmCell *cell = pmCellAlloc(chip);
+    if (cell == NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "pmCellAlloc returned a NULL.\n");
+        return 3;
+    }
+
     pmReadout *readout = pmReadoutAlloc(cell);
     if (readout == NULL) {
@@ -376,4 +366,5 @@
     psFree(cell);
     psFree(readout);
+    psFree(camera);
 
     return 0;
