Index: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 13418)
+++ /trunk/psphot/src/psphotApResid.c	(revision 13419)
@@ -132,4 +132,15 @@
     stats->max = 3.0;
 
+#define P_APTREND_SWITCH_CLEANUP	/* Cleanup memory on error in ApTrendOption switch */ \
+    psFree(psf->growth); psf->growth = NULL; \
+    psFree(mask); \
+    psFree(xPos); \
+    psFree(yPos); \
+    psFree(flux); \
+    psFree(r2rflux); \
+    psFree(apResid); \
+    psFree(dMag); \
+    psFree(stats)
+
     // no correction
     switch (ApTrendOption) {
@@ -143,4 +154,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -150,4 +162,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting aperture correction");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -159,4 +172,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -166,4 +180,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -175,4 +190,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -182,4 +198,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -189,4 +206,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting skysat");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -198,4 +216,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -205,4 +224,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "fitting, XY_LIN");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -214,4 +234,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -221,4 +242,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting XY_QUAD");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -230,4 +252,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -237,4 +260,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting sky xy_lin");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -246,4 +270,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -253,4 +278,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -260,4 +286,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting skyset xy_lin");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -269,4 +296,5 @@
 	if (!psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "Failed to measure apTrend");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -276,4 +304,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "fitting skyBias");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -283,4 +312,5 @@
 	if (!psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux)) {
 	    psError(PSPHOT_ERR_PHOTOM, false, "fitting all");
+	    P_APTREND_SWITCH_CLEANUP;
 	    return false;
 	}
@@ -290,5 +320,6 @@
         return false;
     }
-
+#undef P_APTREND_SWITCH_CLEANUP
+    
     // construct the fitted values and the residuals
     psVector *fit   = psPolynomial4DEvalVector (psf->ApTrend, xPos, yPos, r2rflux, flux);
