Index: /trunk/ppImage/src/ppImage.c
===================================================================
--- /trunk/ppImage/src/ppImage.c	(revision 6395)
+++ /trunk/ppImage/src/ppImage.c	(revision 6396)
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include "pslib.h"
+#include "pmConcepts.h"
 #include "ppImage.h"
 #include "ppMem.h"
-
 
 int main(int argc, char **argv)
@@ -23,12 +23,4 @@
     ppImageParseCamera(data, config);
 
-    // Cleaning up
-    psFree(data);
-    psFree(options);
-    psFree(config);
-    psTimerStop();
-    ppMemCheck();
-#if 0
-
     // Set various tasks (define optional operations)
     ppImageOptionsParse(data, options, config);
@@ -39,6 +31,14 @@
     // Image Arithmetic Loop
     ppImageLoop(data, options, config);
-#endif
 
-    exit(EXIT_SUCCESS);
+    // Cleaning up
+    psFree(data);
+    psFree(options);
+    psFree(config);
+    psTimerStop();
+    psTraceReset();
+    pmConceptsDone();
+    ppMemCheck();
+
+    return EXIT_SUCCESS;
 }
Index: /trunk/ppImage/src/ppImageLoop.c
===================================================================
--- /trunk/ppImage/src/ppImageLoop.c	(revision 6395)
+++ /trunk/ppImage/src/ppImageLoop.c	(revision 6396)
@@ -7,4 +7,5 @@
 #include "ppImageDetrend.h"
 #include "ppImage.h"
+#include "ppMem.h"
 
 #define MEM_LEAKS 1
@@ -101,10 +102,8 @@
 
             // Need to free detrend cells here so we have enough memory to do other stuff
-#ifndef MEM_LEAKS
             psFree(detrend.bias);
             psFree(detrend.dark);
             psFree(detrend.mask);
             psFree(detrend.flat);
-#endif
         }
 
@@ -114,5 +113,5 @@
         // XXX EAM: Photometry goes here!
 
-        // A kludge to get the write to behave wrt the concepts --- we've changed the camera format, so
+        // XXX A kludge to get the write to behave w.r.t. the concepts --- we've changed the camera format, so
         // the concepts don't know what on earth to do.
         const psMetadata *camera = data->input->fpa->camera;
@@ -122,43 +121,7 @@
 
         // Now I can blow away the mosaic so I can then read more.
-#ifndef MEM_LEAKS
-        pmChipFreeCells(inputChip);
-#endif
+        psFree(inputChip);
     }
-
-#if 0
-    // Write out the mosaicked chip, just to see; this wouldn't normally happen
-    psFits *mosaicFile = psFitsOpen("mosaic.fits", "w");
-    for (int i = 0; i < chips->n; i++) {
-        pmChip *chip = chips->data[i];
-        if (! chip || ! chip->exists || ! chip->process) {
-            continue;
-        }
-#if 0
-        psArray *cells = chip->cells;
-        pmCell *cell = cells->data[0];
-        psArray *readouts = cell->readouts;
-        pmReadout *readout = readouts->data[0];
-        psImage *image = readout->image;
-#else
-        psImage *image = chip->hdu->images->data[0];
-#endif
-        psFitsWriteImage(mosaicFile, NULL, image, 0);
-
-    }
-#endif
-
-    // Free the detrend FPA and chips
-#ifndef MEM_LEAKS
-    psFree(data->bias);
-    psFree(data->dark);
-    psFree(data->mask);
-    psFree(data->flat);
-#endif
-
-    psFitsClose(data->output);
 
     return true;
 }
-
-/* if we allow per-readout loads, we need to load detrend images here */
Index: /trunk/ppImage/src/ppImageOptions.c
===================================================================
--- /trunk/ppImage/src/ppImageOptions.c	(revision 6395)
+++ /trunk/ppImage/src/ppImageOptions.c	(revision 6396)
@@ -180,4 +180,5 @@
         // Fill in the options
         options->overscan = pmOverscanOptionsAlloc(overscanSingle, overscanFit, overscanOrder, overscanStats);
+        psFree(overscanStats);
     }
 
Index: /trunk/ppImage/src/ppMem.c
===================================================================
--- /trunk/ppImage/src/ppMem.c	(revision 6395)
+++ /trunk/ppImage/src/ppMem.c	(revision 6396)
@@ -53,7 +53,7 @@
 
     psMemBlock **leaks = NULL;          // List of leaks
-    int nLeaks = psMemCheckLeaks(0, &leaks, leakFile, false); // Number of leaks
+    int nLeaks = psMemCheckLeaks(0, &leaks, leakFile, true); // Number of leaks
     fprintf(stderr, "%d leaks found:\n", nLeaks);
-    for (int i = nLeaks - 1; i > 0; i--) {
+    for (int i = nLeaks - 1; i >= 0; i--) {
         ppMemPrint(leaks[i]);
     }
@@ -63,2 +63,23 @@
     fprintf(stderr, "%d memory blocks corrupted.\n", nCorrupted);
 }
+
+void ppMemUsed(void)
+{
+    psMemBlock **leaks = NULL;          // List of leaks
+    int nLeaks = psMemCheckLeaks(0, &leaks, NULL, true); // Number of leaks
+    size_t total = 0;                   // Total size
+    size_t largest = 0;                 // Largest block size
+    psMemId index = -1;                 // Id number of largest block
+    for (int i = nLeaks - 1; i >= 0; i--) {
+        psMemBlock *mb = leaks[i];
+        if (mb->userMemorySize >= largest) {
+            largest = mb->userMemorySize;
+            index = mb->id;
+        }
+        total += mb->userMemorySize;
+    }
+    psFree(leaks);
+
+    psLogMsg(__func__, PS_LOG_INFO, "Memory used: %ld\nLargest id: %ld (%ld bytes)", total, index, largest);
+
+}
Index: /trunk/ppImage/src/ppMem.h
===================================================================
--- /trunk/ppImage/src/ppMem.h	(revision 6395)
+++ /trunk/ppImage/src/ppMem.h	(revision 6396)
@@ -8,4 +8,5 @@
 void ppMemPrint(const psMemBlock *ptr);
 void ppMemCheck(void);
+void ppMemUsed(void);
 
 #endif
