Index: trunk/Ohana/src/delstar/include/delstar.h
===================================================================
--- trunk/Ohana/src/delstar/include/delstar.h	(revision 39578)
+++ trunk/Ohana/src/delstar/include/delstar.h	(revision 39579)
@@ -83,5 +83,6 @@
 char        *SINGLE_CPT;
 
-int SKIP_IMAGES;
+int   SAVE_DUPLICATES;
+int   SKIP_IMAGES;
 
 time_t    START;
@@ -138,4 +139,7 @@
 int args_client (int argc, char **argv);
 
+void delstar_args_free ();
+void delstar_client_args_free ();
+
 void SortAveMeasMatch (off_t *MEAS, off_t *AVE, off_t N);
 
Index: trunk/Ohana/src/delstar/src/ConfigInit.c
===================================================================
--- trunk/Ohana/src/delstar/src/ConfigInit.c	(revision 39578)
+++ trunk/Ohana/src/delstar/src/ConfigInit.c	(revision 39579)
@@ -54,4 +54,5 @@
   }
 
+  FreeConfigFile();
   free (config);
   free (file);
Index: trunk/Ohana/src/delstar/src/args.c
===================================================================
--- trunk/Ohana/src/delstar/src/args.c	(revision 39578)
+++ trunk/Ohana/src/delstar/src/args.c	(revision 39579)
@@ -194,4 +194,11 @@
     SKIP_IMAGES = TRUE; // we do not need to load the images for -dup-measures
   }
+
+  SAVE_DUPLICATES = FALSE;
+  if ((N = get_argument (argc, argv, "-save-duplicates"))) {
+    SAVE_DUPLICATES = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
   if ((N = get_argument (argc, argv, "-fix-LAP"))) {
     if (MODE != MODE_NONE) usage();
@@ -316,4 +323,14 @@
 }
 
+void delstar_args_free () {
+  FREE (SINGLE_CPT);
+  FREE (IMAGENAME);
+  FREE (UNIQUER);
+  FREE (CATDIR);
+  FREE (EDGE_DELETIONS);
+  FREE (PHOTCODE_LIST);
+  FREE (IMSTATS_FILE);
+}
+
 int args_client (int argc, char **argv) {
   
@@ -453,4 +470,10 @@
   }
 
+  SAVE_DUPLICATES = FALSE;
+  if ((N = get_argument (argc, argv, "-save-duplicates"))) {
+    SAVE_DUPLICATES = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
   MEASURE_EDGE_FILE = NULL;
   if (MODE == MODE_FIX_LAP_EDGES) {
@@ -484,2 +507,14 @@
   return (TRUE);
 }
+
+void delstar_client_args_free () {
+  FREE (CATDIR);
+  FREE (HOSTDIR);
+  FREE (SINGLE_CPT);
+  FREE (IMAGES);
+  FREE (PHOTCODE_LIST);
+  FREE (EDGE_DELETIONS);
+  FREE (MEASURE_EDGE_FILE);
+  FREE (IMSTATS_FILE);
+}
+
Index: trunk/Ohana/src/delstar/src/delete_duplicate_measures.c
===================================================================
--- trunk/Ohana/src/delstar/src/delete_duplicate_measures.c	(revision 39578)
+++ trunk/Ohana/src/delstar/src/delete_duplicate_measures.c	(revision 39579)
@@ -31,4 +31,9 @@
   if (PARALLEL && !HOST_ID) {
     int status = delete_duplicate_measures_parallel (skylist);
+
+    SkyTableFree (sky);
+    SkyListFree (skylist);
+    FreePhotcodeTable ();
+
     return status;
   }
@@ -79,4 +84,9 @@
     dvo_catalog_free (&catalog);
   }
+
+  SkyTableFree (sky);
+  SkyListFree (skylist);
+  FreePhotcodeTable ();
+
   return TRUE;
 }
@@ -117,18 +127,21 @@
     table->hosts[i].pathname = tmppath;
 
-    char command[1024];
-    snprintf (command, 1024, "delstar_client -hostID %d -D CATDIR %s -hostdir %s -region %f %f %f %f -dup-measures", 
+    char *command = NULL;
+    strextend (&command, "delstar_client -hostID %d -D CATDIR %s -hostdir %s -region %f %f %f %f -dup-measures", 
 	      table->hosts[i].hostID, CATDIR, table->hosts[i].pathname, 
 	      UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
 
-    char tmpline[1024];
-    if (VERBOSE)    { snprintf (tmpline, 1024, "%s -v",      command);             strcpy (command, tmpline); }
-    if (VERBOSE2)   { snprintf (tmpline, 1024, "%s -vv",     command);             strcpy (command, tmpline); }
-    if (SINGLE_CPT) { snprintf (tmpline, 1024, "%s -cpt %s", command, SINGLE_CPT); strcpy (command, tmpline); }
-    if (UPDATE)     { snprintf (tmpline, 1024, "%s -update", command);             strcpy (command, tmpline); }
+    if (VERBOSE)    	 { strextend (&command, "-v");                  }
+    if (VERBOSE2)   	 { strextend (&command, "-vv");                 }
+    if (SINGLE_CPT) 	 { strextend (&command, "-cpt %s", SINGLE_CPT); }
+    if (UPDATE)     	 { strextend (&command, "-update");             }
+    if (SAVE_DUPLICATES) { strextend (&command, "-save-duplicates");    }
 
     fprintf (stderr, "command: %s\n", command);
 
-    if (PARALLEL_MANUAL) continue;
+    if (PARALLEL_MANUAL) {
+      free (command);
+      continue;
+    }
 
     if (PARALLEL_SERIAL) {
@@ -148,4 +161,5 @@
       table->hosts[i].pid = pid; // save for future reference
     }
+    free (command);
   }
 
@@ -157,4 +171,6 @@
     HostTableWaitJobsGetIO (table, __FILE__, __LINE__, VERBOSE);
   }
+
+  FreeHostTable(table);
 
   return TRUE;
@@ -211,4 +227,9 @@
   memset (duplicates,  0, sizeof(int) * Nmeasure);
 
+  for (i = 0; i < Naverage; i++) {
+    averageNmeas[i] = 0;
+    averageDmeas[i] = -1;
+  }
+
   // make an array of the full IDs
   for (i = 0; i < Nmeasure; i++) {
@@ -236,4 +257,22 @@
   }
     
+  FILE *fsave = NULL;
+  if (SAVE_DUPLICATES) {
+    char savename[DVO_MAX_PATH];
+    snprintf (savename, DVO_MAX_PATH, "%s.save", catalog->filename);
+    struct stat filestat;
+    int myStatus = stat (savename, &filestat);
+    if (myStatus && (errno == ENOENT)) { 
+      fsave = fopen (savename, "w");
+      if (!fsave) {
+	fprintf (stderr, "problem opening file %s for output\n", savename);
+      }
+    } else {
+      if (!myStatus) {
+	fprintf (stderr, "file %s exists: will not overwrite\n", savename);
+      }
+    }
+  }    
+
   // mark the measures to be dropped
   for (i = 0; i < Nmeasure; i++) {
@@ -243,5 +282,10 @@
     off_t N = measure[j].averef;
     if (VERBOSE) fprintf (stderr, "0x%08x 0x%08x %8.4f %8.4f %5d\n", measure[j].imageID, measure[j].detID, average[N].R, average[N].D, measure[j].photcode);
-  }
+    if (fsave) {
+      fprintf (fsave, "0x%08x 0x%08x %8.4f %8.4f %5d\n", measure[j].imageID, measure[j].detID, average[N].R, average[N].D, measure[j].photcode);
+    }
+  }
+  
+  if (fsave) fclose (fsave);
 
   // set up the measure sequence lists
@@ -267,4 +311,6 @@
   // n = measureRefOut[i] : measureOut[n] = measure[i]
   ALLOCATE (measureAveOut, off_t, NmeasOut);
+
+
 
   // count the number of measures for each averef
@@ -293,7 +339,8 @@
 
   // generate the new average sequence, skipping entries with no measurements
+  // update 20160524 : keep entries with no measurements
   off_t NaveOut = 0;
   for (i = 0; i < Naverage; i++) {
-    if (averageNmeas[i] == 0) continue;
+    // if (averageNmeas[i] == 0) continue;
     averageSeqOut[NaveOut] = i;
     averageRefOut[i] = NaveOut;
Index: trunk/Ohana/src/delstar/src/delstar.c
===================================================================
--- trunk/Ohana/src/delstar/src/delstar.c	(revision 39578)
+++ trunk/Ohana/src/delstar/src/delstar.c	(revision 39579)
@@ -27,4 +27,7 @@
     case MODE_DUP_MEASURES:
       if (!delete_duplicate_measures ()) exit (1);
+      delstar_args_free ();
+      ohana_memcheck (TRUE);
+      ohana_memdump (TRUE);
       exit (0);
       break;
Index: trunk/Ohana/src/delstar/src/delstar_client.c
===================================================================
--- trunk/Ohana/src/delstar/src/delstar_client.c	(revision 39578)
+++ trunk/Ohana/src/delstar/src/delstar_client.c	(revision 39579)
@@ -23,8 +23,14 @@
       }
       if (!delete_duplicate_image_measures (imageID)) exit (1);
+      delstar_client_args_free ();
+      ohana_memcheck (TRUE);
+      ohana_memdump (TRUE);
       exit (0);
       break;
     case MODE_DUP_MEASURES:
       if (!delete_duplicate_measures ()) exit (1);
+      delstar_client_args_free ();
+      ohana_memcheck (TRUE);
+      ohana_memdump (TRUE);
       exit (0);
       break;
