Index: /trunk/psLib/src/math/psStats.c
===================================================================
--- /trunk/psLib/src/math/psStats.c	(revision 10273)
+++ /trunk/psLib/src/math/psStats.c	(revision 10274)
@@ -16,6 +16,6 @@
  * use ->min and ->max (PS_STAT_USE_RANGE)
  *
- *  @version $Revision: 1.190 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-11-23 05:33:28 $
+ *  @version $Revision: 1.191 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-11-29 17:55:25 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -1103,5 +1103,4 @@
         psError(PS_ERR_UNKNOWN, false, "Failed to calculate the min/max of the input vector.\n");
         psFree(statsMinMax);
-        psFree(mask);
         psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
         return false;
@@ -1157,5 +1156,4 @@
         psFree(statsMinMax);
         psFree(histogram);
-        psFree(mask);
         psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
         return false;
@@ -1195,9 +1193,9 @@
         psError(PS_ERR_UNKNOWN, false, "Failed to fit a gaussian to the robust histogram.\n");
         psFree(statsMinMax);
+        psFree(histogram);
         psFree(x);
         psFree(y);
         psFree(minimizer);
         psFree(params);
-        psFree(mask);
         psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
         return false;
@@ -1294,5 +1292,4 @@
             psError(PS_ERR_UNKNOWN, false, "Failed to calculate the min/max of the input vector.\n");
             psFree(statsMinMax);
-            psFree(mask);
             psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
             return false;
@@ -1312,5 +1309,4 @@
             stats->robustN50 = numValid;
             psFree(statsMinMax);
-            psFree(mask);
 
             psTrace("psLib.math", 4, "---- %s(0) end  ----\n", __func__);
@@ -1371,5 +1367,4 @@
                 psFree(histogram);
                 psFree(cumulative);
-                psFree(mask);
                 psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
                 return false;
@@ -1388,5 +1383,4 @@
             psFree(histogram);
             psFree(cumulative);
-            psFree(mask);
             psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
             return false;
@@ -1424,5 +1418,4 @@
             psFree(histogram);
             psFree(cumulative);
-            psFree(mask);
             psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
             return false;
@@ -1543,5 +1536,4 @@
         psFree(statsMinMax);
         psFree(cumulative);
-        psFree(mask);
         psTrace("psLib.math", 4, "---- %s(false) end  ----\n", __func__);
         return false;
@@ -1561,5 +1553,4 @@
                 "could not determine the robustUQ: fitQuadraticSearchForYThenReturnX() returned a NAN.\n");
         psFree(statsMinMax);
-        psFree(mask);
         psTrace("psLib.math", 4, "---- %s(1) end  ----\n", __func__);
         return false;
@@ -1584,8 +1575,20 @@
         stats->fittedStdev = stats->robustStdev;  // pass the guess sigma
         stats->fittedMean = stats->robustMedian;  // pass the guess mean
-        vectorFittedStats (myVector, errors, mask, maskVal, stats);
+        if (!vectorFittedStats(myVector, errors, mask, maskVal, stats)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to estimate statistics (pass 1)");
+            psFree(statsMinMax);
+            psFree(mask);
+
+            return false;
+        }
         // if there is a large swing in sigma, try a second time
         if ((stats->fittedStdev/stats->robustStdev) < 0.75) {
-            vectorFittedStats (myVector, errors, mask, maskVal, stats);
+            if (!vectorFittedStats (myVector, errors, mask, maskVal, stats)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to estimate statistics (pass 2)");
+                psFree(statsMinMax);
+                psFree(mask);
+
+                return false;
+            }
         }
     }
@@ -1966,5 +1969,5 @@
     The stats structure.
  
-XXX: Should we free stats if the asserts fail?
+XXX: Should we free stats if the asserts fail? NO; we don't own it (RHL).
  *****************************************************************************/
 psStats* psVectorStats(
@@ -2052,5 +2055,4 @@
         if (!vectorRobustStats(inF32, errorsF32, maskU8, maskVal, stats)) {
             psError(PS_ERR_UNKNOWN, false, _("Failed to calculate the specified statistic."));
-            psFree(stats);
             psFree(inF32);
             psFree(errorsF32);
