Index: /trunk/psphot/src/psphotChoosePSF.c
===================================================================
--- /trunk/psphot/src/psphotChoosePSF.c	(revision 16945)
+++ /trunk/psphot/src/psphotChoosePSF.c	(revision 16946)
@@ -22,5 +22,5 @@
     pmPSFOptions *options = pmPSFOptionsAlloc ();
 
-    // load user options from the recipe. no need to check existence -- they are 
+    // load user options from the recipe. no need to check existence -- they are
     // array to store candidate PSF stars
     int NSTARS = psMetadataLookupS32 (&status, recipe, "PSF_MAX_NSTARS");
@@ -49,5 +49,5 @@
     options->psfTrendMode = pmTrend2DModeFromString (psMetadataLookupStr (&status, recipe, "PSF.TREND.MODE"));
     assert (status);
-    
+
     options->psfTrendNx = psMetadataLookupS32 (&status, recipe, "PSF.TREND.NX");
     assert (status);
@@ -101,36 +101,38 @@
     } else {
         if (item->type != PS_DATA_METADATA_MULTI) psAbort("missing PSF_MODEL selection");
-	modelNames = psListToArray (item->data.list);
+        modelNames = psListToArray (item->data.list);
     }
 
     // generate a psf model using the first selection
     if (stars->n == 0) {
-	// XXX optionally give up here
-	psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
-	psErrorStackPrint (stderr, "Using guess PSF model");
-	psErrorClear ();
-	
-	// unset the PSFSTAR flags (none are used):
-	for (int i = 0; i < sources->n; i++) {
-	    pmSource *source = sources->data[i];
-	    source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-	}
-
-	// XXX set sxx, etc from FWHM in recipe
-	pmPSF *psf = pmPSFBuildSimple (modelNames->data[0], 1.0, 1.0, 0.0, 1.0);
-	psf->fieldNx = readout->image->numCols;
-	psf->fieldNy = readout->image->numRows;
-	psFree (modelNames);
-
-	bool status = true;
-	status &= psphotMakeFluxScale (readout->image, recipe, psf);
-	status &= psphotPSFstats (readout, recipe, psf);
-	if (!status) {
-	    psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
-	    psFree (psf);
-	    return NULL;
-	}
-
-	// XXX set DSX_MEAN, etc?
+        // XXX optionally give up here
+        psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
+        psErrorStackPrint (stderr, "Using guess PSF model");
+        psErrorClear ();
+
+        psFree(options);
+
+        // unset the PSFSTAR flags (none are used):
+        for (int i = 0; i < sources->n; i++) {
+            pmSource *source = sources->data[i];
+            source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
+        }
+
+        // XXX set sxx, etc from FWHM in recipe
+        pmPSF *psf = pmPSFBuildSimple (modelNames->data[0], 1.0, 1.0, 0.0, 1.0);
+        psf->fieldNx = readout->image->numCols;
+        psf->fieldNy = readout->image->numRows;
+        psFree (modelNames);
+
+        bool status = true;
+        status &= psphotMakeFluxScale (readout->image, recipe, psf);
+        status &= psphotPSFstats (readout, recipe, psf);
+        if (!status) {
+            psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
+            psFree (psf);
+            return NULL;
+        }
+
+        // XXX set DSX_MEAN, etc?
         return psf;
     }
@@ -171,34 +173,36 @@
     if (bestN < 0) {
         psFree (models);
-	psFree (stars);
-
-	// XXX optionally give up here
-	// generate a psf model using the first selection
-	psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
-	psErrorStackPrint (stderr, "Using guess PSF model");
-	psErrorClear ();
-	
-	// unset the PSFSTAR flags (none are used):
-	for (int i = 0; i < sources->n; i++) {
-	    pmSource *source = sources->data[i];
-	    source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-	}
-
-	// XXX set sxx, etc from FWHM in recipe
-	pmPSF *psf = pmPSFBuildSimple (modelNames->data[0], 1.0, 1.0, 0.0, 1.0);
-	psf->fieldNx = readout->image->numCols;
-	psf->fieldNy = readout->image->numRows;
-	psFree (modelNames);
-
-	bool status = true;
-	status &= psphotMakeFluxScale (readout->image, recipe, psf);
-	status &= psphotPSFstats (readout, recipe, psf);
-	if (!status) {
-	    psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
-	    psFree (psf);
-	    return NULL;
-	}
-
-	// XXX set DSX_MEAN, etc?
+        psFree (stars);
+
+        // XXX optionally give up here
+        // generate a psf model using the first selection
+        psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
+        psErrorStackPrint (stderr, "Using guess PSF model");
+        psErrorClear ();
+
+        psFree(options);
+
+        // unset the PSFSTAR flags (none are used):
+        for (int i = 0; i < sources->n; i++) {
+            pmSource *source = sources->data[i];
+            source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
+        }
+
+        // XXX set sxx, etc from FWHM in recipe
+        pmPSF *psf = pmPSFBuildSimple (modelNames->data[0], 1.0, 1.0, 0.0, 1.0);
+        psf->fieldNx = readout->image->numCols;
+        psf->fieldNy = readout->image->numRows;
+        psFree (modelNames);
+
+        bool status = true;
+        status &= psphotMakeFluxScale (readout->image, recipe, psf);
+        status &= psphotPSFstats (readout, recipe, psf);
+        if (!status) {
+            psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
+            psFree (psf);
+            return NULL;
+        }
+
+        // XXX set DSX_MEAN, etc?
         return psf;
     }
@@ -280,4 +284,5 @@
         psError(PSPHOT_ERR_PSF, false, "Unable to construct residual table for PSF");
         psFree (models);
+        psFree(options);
         return NULL;
     }
@@ -287,4 +292,5 @@
         psError(PSPHOT_ERR_PSF, false, "Unable to construct flux scale for PSF");
         psFree (models);
+        psFree(options);
         return NULL;
     }
@@ -354,5 +360,5 @@
         pmSourcesWritePSFs (try->sources, "psfstars.dat");
         pmSourcesWriteEXTs (try->sources, "extstars.dat", false);
-	// XXX need alternative output function
+        // XXX need alternative output function
         // psMetadata *psfData = pmPSFtoMetadata (NULL, try->psf);
         // psMetadataConfigWrite (psfData, "psfmodel.dat");
@@ -369,4 +375,5 @@
     if (!psphotPSFstats (readout, recipe, psf)) {
         psError(PSPHOT_ERR_PSF, false, "cannot measure PSF shape terms");
+        psFree(options);
         psFree(psf);
         return NULL;
