Index: trunk/psphot/src/psphotMakeFluxScale.c
===================================================================
--- trunk/psphot/src/psphotMakeFluxScale.c	(revision 18853)
+++ trunk/psphot/src/psphotMakeFluxScale.c	(revision 18854)
@@ -14,5 +14,5 @@
 
     pmTrend2D *trend = pmTrend2DAlloc (PM_TREND_MAP, image, Nx, Ny, stats);
-    
+
     psVector *xPts = psVectorAlloc (Nx*Ny, PS_TYPE_F32);
     psVector *yPts = psVectorAlloc (Nx*Ny, PS_TYPE_F32);
@@ -20,44 +20,55 @@
 
     int Npts = 0;
+    bool success = true;                // Function succeeded?
 
-    // generate a set of test normalized PSF fluxes filling the grid 
+    // generate a set of test normalized PSF fluxes filling the grid
     for (int ix = 0; ix < Nx; ix++) {
-	for (int iy = 0; iy < Ny; iy++) {
-	    
-	    float x = psImageBinningGetFineX (trend->map->binning, ix + 0.5);
-	    float y = psImageBinningGetFineY (trend->map->binning, iy + 0.5);
+        for (int iy = 0; iy < Ny; iy++) {
 
-	    // create normalized model object at xc,yc 
-	    pmModel *model = pmModelFromPSFforXY (psf, x, y, 1.0);
+            float x = psImageBinningGetFineX (trend->map->binning, ix + 0.5);
+            float y = psImageBinningGetFineY (trend->map->binning, iy + 0.5);
 
-	    // measure the fitMag for this model
-	    float fitSum = model->modelFlux (model->params);
-	    assert (fitSum > 0);
-	    assert (isfinite(fitSum));
+            // create normalized model object at xc,yc
+            pmModel *model = pmModelFromPSFforXY (psf, x, y, 1.0);
+            if (!model) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to generate model for grid point %d,%d", ix, iy);
+                success = false;
+                goto DONE;
+            }
 
-	    xPts->data.F32[Npts] = x;
-	    yPts->data.F32[Npts] = y;
-	    fPts->data.F32[Npts] = fitSum;
-	    Npts ++;
-	    assert (Npts <= xPts->nalloc);
-	    psFree (model);
-	}
+            // measure the fitMag for this model
+            float fitSum = model->modelFlux (model->params);
+            assert (fitSum > 0);
+            assert (isfinite(fitSum));
+
+            xPts->data.F32[Npts] = x;
+            yPts->data.F32[Npts] = y;
+            fPts->data.F32[Npts] = fitSum;
+            Npts ++;
+            assert (Npts <= xPts->nalloc);
+            psFree (model);
+        }
     }
     xPts->n = Npts;
     yPts->n = Npts;
     fPts->n = Npts;
-    
-    // XXX test for errors here
-    pmTrend2DFit (trend, NULL, 0xff, xPts, yPts, fPts, NULL);
-    
+
+    if (!pmTrend2DFit (trend, NULL, 0xff, xPts, yPts, fPts, NULL)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to fit trend");
+        success = false;
+        goto DONE;
+    }
+
     // XXX do something useful to measure residual statistics
 
-    psf->FluxScale = trend;
+    psf->FluxScale = psMemIncrRefCounter(trend);
 
-    psFree (xPts);
-    psFree (yPts);
-    psFree (fPts);
-    psFree (stats);
+ DONE:
+    psFree(xPts);
+    psFree(yPts);
+    psFree(fPts);
+    psFree(stats);
+    psFree(trend);
 
-    return true;
+    return success;
 }
