Index: /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 15249)
+++ /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 15250)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-10-08 18:52:49 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-10-09 03:10:05 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -66,5 +66,5 @@
     for (i = 0; i < sources->n; i++) {
         pmSource *source = (pmSource *) sources->data[i];
-	source->seq = i;
+        source->seq = i;
 
         // no difference between PSF and non-PSF model
@@ -166,8 +166,7 @@
     assert (modelType > -1);
 
-    psArray *table = psFitsReadTable (fits);
     // validate a single row of the table (must match SMP)
 
-    // XXX test return values 
+    // XXX test return values
 
     // XXX we have a memory problem, which is illustrated here: if I allocate the sources array
@@ -178,9 +177,16 @@
     // return (fooSources);
 
-    psArray *sources = psArrayAlloc (table->n);
+
+    // We get the size of the table, and allocate the array of sources first because the table is large and
+    // ephemeral --- when the table gets blown away, whatever is allocated after the table is read.  In fact,
+    // it's better to read the table row by row.
+    long numSources = psFitsTableSize(fits); // Number of sources in table
+    psArray *sources = psArrayAlloc(numSources); // Array of sources, to return
 
     // convert the table to the pmSource entries
     // XXX need to chooose PSF vs EXT, based on type?
-    for (int i = 0; i < table->n; i++) {
+    for (int i = 0; i < numSources; i++) {
+        psMetadata *row = psFitsReadTableRow(fits, i); // Table row
+
         pmSource *source = pmSourceAlloc ();
         pmModel *model = pmModelAlloc (modelType);
@@ -191,6 +197,4 @@
         PAR = model->params->data.F32;
         dPAR = model->dparams->data.F32;
-
-        psMetadata *row = table->data[i];
 
         PAR[PM_PAR_XPOS]  = psMetadataLookupF32 (&status, row, "X_PSF");
@@ -225,5 +229,5 @@
 
         source->mode      = psMetadataLookupU16 (&status, row, "FLAGS");
-	assert (status);
+        assert (status);
 
         // XXX other values saved but not loaded?
@@ -232,6 +236,7 @@
 
         sources->data[i] = source;
-    }
-    psFree (table);
-    return (sources);
+        psFree(row);
+    }
+
+    return sources;
 }
