Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 13830)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 13837)
@@ -42,4 +42,5 @@
     // files associated with the science image
     if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	psFree (view);
         return false;
     }
@@ -52,4 +53,5 @@
 
         if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	    psFree (view);
             return false;
         }
@@ -61,4 +63,5 @@
             }
             if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+		psFree (view);
                 return false;
             }
@@ -75,4 +78,5 @@
             while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
                 if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+		    psFree (view);
                     return false;
                 }
@@ -84,4 +88,5 @@
                 if (!ppImageDetrendReadout(config, options, view)) {
                     psError(PS_ERR_UNKNOWN, false, "Unable to detrend readout.\n");
+		    psFree (view);
                     return false;
                 }
@@ -94,4 +99,5 @@
             pmChip *fringe = pmFPAfileThisChip(config->files, view, "PPIMAGE.FRINGE");
             if (!ppImageDetrendFringeSolve(chip, fringe, options)) {
+		psFree (view);
                 return false;
             }
@@ -110,4 +116,5 @@
                 pmCell *fringeCell = pmFPAfileThisCell(config->files, view, "PPIMAGE.FRINGE");
                 if (!ppImageDetrendFringeGenerate(cell, fringeCell)) {
+		    psFree (view);
                     return false;
                 }
@@ -120,14 +127,16 @@
                 if (!mdok || !output) {
                     psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find file PPIMAGE.OUTPUT.\n");
+		    psFree (view);
                     return false;
                 }
 
                 if (!ppStats(stats, 
-			    output->fpa, 
-			    view,
-			    options->satMask | options->badMask | options->maskValue,
-			    config)) {
+			     output->fpa, 
+			     view,
+			     options->satMask | options->badMask | options->maskValue,
+			     config)) {
                     psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to generate stats for image.\n");
 		    psFree (stats);
+		    psFree (view);
                     return false;
                 }
@@ -151,23 +160,19 @@
                 psFree(headerName);
             }
-
-#if 0
-            if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
-                return false;
-            }
-#endif
-
         }
 
         if (!ppImageMosaicChip(config, view, "PPIMAGE.OUTPUT.CHIP", "PPIMAGE.OUTPUT")) {
             psError(PS_ERR_UNKNOWN, false, "Unable to mosaic chip.\n");
+	    psFree (view);
             return false;
         }
         if (!ppImageRebinChip(config, view, "PPIMAGE.BIN1")) {
             psError(PS_ERR_UNKNOWN, false, "Unable to bin chip.\n");
+	    psFree (view);
             return false;
         }
         if (!ppImageRebinChip(config, view, "PPIMAGE.BIN2")) {
             psError(PS_ERR_UNKNOWN, false, "Unable to bin chip.\n");
+	    psFree (view);
             return false;
         }
@@ -175,5 +180,9 @@
         // we perform photometry on the readouts of this chip in the output
         if (options->doPhotom) {
-            ppImagePhotom(config, view);
+            if (!ppImagePhotom(config, view)) {
+		psError(PS_ERR_UNKNOWN, false, "error running photometry.\n");
+		psFree (view);
+		return false;
+	    }
         }
 
@@ -186,4 +195,5 @@
 
             if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+		psFree(view);
                 return false;
             }
@@ -192,4 +202,5 @@
         // Close chip
         if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	    psFree(view);
             return false;
         }
@@ -198,5 +209,5 @@
     // Write out summary statistics
     if (stats) {
-        const char *statsMDC = psMetadataConfigFormat(stats);
+        char *statsMDC = psMetadataConfigFormat(stats);
         if (!statsMDC || strlen(statsMDC) == 0) {
             psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
@@ -204,5 +215,5 @@
             fprintf(statsFile, "%s", statsMDC);
         }
-        psFree((void *)statsMDC);
+        psFree(statsMDC);
         fclose(statsFile);
         psFree(stats);
@@ -217,20 +228,18 @@
     // we perform astrometry on all chips after sources have been detected
     if (options->doAstromChip || options->doAstromMosaic) {
-        ppImageAstrom(config);
-    }
-    // XXX should we keep the addstar command as an option, or just run it externally?
-    // XXX if we included direct Ohana/DVO API calls, we could send the data from within ppImage.
-# if (0)    
-    if (options->doAddstar) {
-        ppImageAddstar(config);
-    }
-# endif
-
+        if (!ppImageAstrom(config)) {
+	    psError(PS_ERR_UNKNOWN, false, "error running astrometry.\n");
+	    psFree(view);
+	    return false;
+	}
+    }
+
+    // Close FPA
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psFree(view);
         return false;
     }
 
     psFree(view);
-
     return true;
 }
