Index: trunk/psModules/src/detrend/pmShutterCorrection.c
===================================================================
--- trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 9992)
+++ trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 9997)
@@ -400,5 +400,5 @@
             continue;
         }
-        images->data[i] = image;
+        images->data[i] = psMemIncrRefCounter(image);
         if (image->type.type != PS_TYPE_F32) {
             psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for image: %x\n", image->type.type);
@@ -429,5 +429,5 @@
                 goto MEASURE_ERROR;
             }
-            masks->data[i] = mask;
+            masks->data[i] = psMemIncrRefCounter(mask);
 
             if (mask->type.type != PS_TYPE_U8) {
@@ -452,7 +452,7 @@
                 goto MEASURE_ERROR;
             }
-            masks->data[i] = mask;
-
-            if (mask->type.type != PS_TYPE_F32) {
+            weights->data[i] = psMemIncrRefCounter(weight);
+
+            if (weight->type.type != PS_TYPE_F32) {
                 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for weights: %x\n", weight->type.type);
                 goto MEASURE_ERROR;
@@ -484,5 +484,9 @@
             psRegion *region = regions->data[j]; // Region of interest
             psImage *subImage = psImageSubset(image, *region); // Sub-image
-            if (!psImageStats(stats, subImage, masks->data[i], maskVal)) {
+            psImage *subMask = NULL;
+            if (mask) {
+                subMask = psImageSubset(mask, *region);
+            }
+            if (!psImageStats(stats, subImage, subMask, maskVal)) {
                 psString regionString = psRegionToString(*region);
                 psWarning("Unable to measure sample statistics at %s in image %ld.\n",
