Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 41503)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 41504)
@@ -308,4 +308,6 @@
     psFitsOptions *options = fits->options; // Options for FITS writing
 
+    // psMemCheckCorruption (stderr, true); // 1
+
     // Can't PLIO compress U16,U32,U64 directly, so convert them
     if (psFitsCompressionGetType(fits) == PS_FITS_COMPRESS_PLIO && (!options || options->bitpix == 0)) {
@@ -329,4 +331,6 @@
     }
 
+    // psMemCheckCorruption (stderr, true); // 1x
+
     // Custom floating-point
     if (PS_IS_PSELEMTYPE_REAL(image->type.type) && options->conventions.psBitpix &&
@@ -340,4 +344,6 @@
         return psMemIncrRefCounter((psImage*)image); // Casting away const
     }
+
+    // psMemCheckCorruption (stderr, true); // 2
 
     // Quantise floating-point images
@@ -358,4 +364,7 @@
                 return NULL;
             }
+
+	    // psMemCheckCorruption (stderr, true); // 2x
+
             status = 0;
             if (fits_read_key_dbl(fits->fd, "BZERO", bzero, NULL, &status) && status != KEY_NO_EXIST) {
@@ -369,4 +378,7 @@
                 return NULL;
             }
+
+	    // psMemCheckCorruption (stderr, true); // 3
+
         }
 
@@ -404,4 +416,7 @@
     break;
 
+
+    // psMemCheckCorruption (stderr, true); // 4
+
     *bscale = 1.0;
     *bzero = 0.0;
@@ -421,11 +436,17 @@
     }
 
+    // psMemCheckCorruption (stderr, true); // 3x
+
     if (outType == inType) {
         return psMemIncrRefCounter((psImage*)image);
     }
 
+    // psMemCheckCorruption (stderr, true); // 5
+
     if (PSELEMTYPE_SIZEOF(inType) > PSELEMTYPE_SIZEOF(outType)) {
         psWarning("Truncating image pixels to write to disk.");
     }
+
+    // psMemCheckCorruption (stderr, true); // 4x
 
     return psImageCopy(NULL, image, outType);
@@ -647,4 +668,6 @@
     }
 
+    // fprintf (stderr, "calling psFitsInsertImageWithMask for %s, %lx, %lx\n", fits->fd->Fptr->filename, (long int) image, (long int) mask);
+
     int numCols = image->numCols;       // Number of columns for image
     int numRows = image->numRows;       // Number of rows for image
@@ -659,6 +682,12 @@
     psFitsFloat floatType;              // Custom floating-point convention type
 
+    // psMemCheckCorruption (stderr, true);
+
     psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &bsoften, &blank, &floatType, 
 						   fits, image, mask, maskVal, NULL, true); // Image to write out
+
+    // psMemCheckCorruption (stderr, true);
+
+    // fprintf (stderr, "allocated in psFitsInsertImageWithMask %s, %lx\n", fits->fd->Fptr->filename, (long int) diskImage);
 
     if (!diskImage) {
@@ -721,4 +750,6 @@
         naxis = 2;
     }
+
+    // psMemCheckCorruption (stderr, true);
 
     bool createPHU = false;             // Are we creating a PHU?
@@ -864,4 +895,6 @@
     }
 
+    // psMemCheckCorruption (stderr, true);
+
     if (psFitsError(status, true, "Could not write image to file.")) {
         success = false;
@@ -877,4 +910,6 @@
     }
 
+
+    // psMemCheckCorruption (stderr, true);
 
  INSERT_DONE:
