Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/include/dvomerge.h
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/include/dvomerge.h	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/include/dvomerge.h	(revision 38502)
@@ -116,4 +116,5 @@
 void 	   dvomerge_help          PROTO((int argc, char **argv));
 int  	   dvomerge_args 	  PROTO((int *argc, char **argv));
+void       dvomerge_args_free     PROTO((void));
 
 void       dvomerge_client_usage  PROTO((void));
@@ -175,4 +176,5 @@
 int   	   dvo_update_image_IDs   PROTO((IDmapType *IDmap, Catalog *catalog));
 void       dvo_image_map_free     PROTO((IDmapType *IDmap));
+void       dvo_image_map_init     PROTO((IDmapType *IDmap));
 
 // dvorepair prototypes
@@ -213,4 +215,6 @@
 int dmhObjectCheck (dmhObject *history, dmhObjectStats *inStats);
 dmhObject *dmhObjectRead (char *filename);
+dmhObject *dmhObjectAlloc (void);
+void dmhObjectFree (dmhObject *history);
 
 void dmhObjectStatsFree (dmhObjectStats *stats);
@@ -218,6 +222,7 @@
 
 int dmhImageAdd (FITS_DB *db, dmhImage *history, char *dbID);
-dmhImage *dmhImageRead (FITS_DB *db) ;
+dmhImage *dmhImageRead (FITS_DB *db);
 int dmhImageCheck (dmhImage *history, char *dbID);
+void dmhImageFree(dmhImage *history);
 
 char *dmhImageReadID (FITS_DB *db);
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/ConfigInit.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/ConfigInit.c	(revision 38502)
@@ -47,4 +47,5 @@
   VERBOSE = TRUE;
 
+  FreeConfigFile();
   free (config);
   free (file);
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/args.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/args.c	(revision 38502)
@@ -6,4 +6,7 @@
   int N;
 
+  HOSTDIR = NULL;
+  SINGLE_CPT = NULL;
+
   /* extra error messages */
   VERBOSE = FALSE;
@@ -53,4 +56,5 @@
 
   /* use a different photcode file to define mean values */
+  ALTERNATE_PHOTCODE_FILE = NULL;
   if ((N = get_argument (*argc, argv, "-photcode-file"))) {
     remove_argument (N, argc, argv);
@@ -156,4 +160,12 @@
 }
 
+void dvomerge_args_free (void) {
+  FREE (HOSTDIR);
+  FREE (ALTERNATE_PHOTCODE_FILE);
+  FREE (UPDATE_CATFORMAT);
+  FREE (UPDATE_CATCOMPRESS);
+  FREE (SINGLE_CPT);
+}
+
 /*** check for command line options ***/
 int dvomerge_client_args (int *argc, char **argv) {
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvo_image_merge_dbs.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvo_image_merge_dbs.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvo_image_merge_dbs.c	(revision 38502)
@@ -6,9 +6,17 @@
 off_t getTgtIndex (e_time start, e_time stop, short photcode, off_t *TgtIndex, e_time *TgtTimes, short *TgtCodes, off_t NimagesTgt);
 
+void dvo_image_map_init (IDmapType *IDmap) {
+  // we don't own the IDmap, just the elements
+  IDmap->old = NULL;
+  IDmap->new = NULL;
+  IDmap->notFound = NULL;
+  IDmap->Nmap = 0;
+}
+
 void dvo_image_map_free (IDmapType *IDmap) {
   // we don't own the IDmap, just the elements
-  free (IDmap->old);
-  free (IDmap->old);
-  free (IDmap->notFound);
+  FREE (IDmap->old);
+  FREE (IDmap->new);
+  FREE (IDmap->notFound);
 }
 
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomerge.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomerge.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomerge.c	(revision 38502)
@@ -7,4 +7,6 @@
   ConfigInit (&argc, argv);
   dvomerge_args (&argc, argv);
+
+  if ((argc < 4) || (argc > 6)) dvomerge_usage();
 
   if (argc == 6) {
@@ -38,6 +40,8 @@
     }
   }
-  dvomerge_usage();
-  exit (2); // cannot reach here.
+  dvomerge_args_free ();
+  ohana_memcheck (TRUE);
+  ohana_memdump (TRUE);
+  exit (0);
 }
 
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeHistory.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeHistory.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeHistory.c	(revision 38502)
@@ -28,4 +28,15 @@
   char *ID = strcreate (dbID);
   return ID;
+}
+
+void dmhImageFree(dmhImage *history) {
+  if (!history) return;
+  
+  int i;
+  for (i = 0; i < history->Nmerge; i++) {
+    FREE (history->IDs[i]);
+  }
+  FREE (history->IDs);
+  FREE (history);
 }
 
@@ -125,4 +136,34 @@
 }
 
+dmhObject *dmhObjectAlloc (void) {
+
+  dmhObject *history = NULL;
+  ALLOCATE (history, dmhObject, 1);
+  history->Nmerge = 0;
+  
+  // ALLOCATE full list
+  ALLOCATE (history->size, off_t,  history->Nmerge);
+  ALLOCATE (history->time, time_t, history->Nmerge);
+  ALLOCATE (history->date, char *, history->Nmerge);
+  return history;
+}
+
+void dmhObjectFree (dmhObject *history) {
+  if (!history) return;
+
+  FREE (history->size);
+  FREE (history->time);
+
+  if (history->date) {
+    int i;
+    for (i = 0; i < history->Nmerge; i++) {
+      FREE (history->date[i]);
+    }
+  }
+  FREE (history->date);
+  FREE (history);
+  return;
+}
+
 // read the array of merged history information from this file's header
 dmhObject *dmhObjectRead (char *filename) {
@@ -133,12 +174,5 @@
   if (stat_result) {
     if (errno == ENOENT) {
-      dmhObject *history = NULL;
-      ALLOCATE (history, dmhObject, 1);
-      history->Nmerge = 0;
-    
-      // ALLOCATE full list
-      ALLOCATE (history->size, off_t,  history->Nmerge);
-      ALLOCATE (history->time, time_t, history->Nmerge);
-      ALLOCATE (history->date, char *, history->Nmerge);
+      dmhObject *history = dmhObjectAlloc();
       return history;
     }
@@ -162,6 +196,5 @@
   fclose (fout);
 
-  dmhObject *history = NULL;
-  ALLOCATE (history, dmhObject, 1);
+  dmhObject *history = dmhObjectAlloc();
 
   int status = gfits_scan (&outheader, "NMERGE", "%d", 1, &history->Nmerge);      
@@ -171,7 +204,7 @@
 
   // ALLOCATE full list
-  ALLOCATE (history->size, off_t,  history->Nmerge);
-  ALLOCATE (history->time, time_t, history->Nmerge);
-  ALLOCATE (history->date, char *, history->Nmerge);
+  REALLOCATE (history->size, off_t,  history->Nmerge);
+  REALLOCATE (history->time, time_t, history->Nmerge);
+  REALLOCATE (history->date, char *, history->Nmerge);
 
   int i;
@@ -199,4 +232,5 @@
     history->time[i] = ohana_date_to_sec (date);
   }
+  gfits_free_header (&outheader);
   return history;
 }
@@ -239,5 +273,5 @@
   history->size[last] = inStats->size;
   history->time[last] = inStats->time;
-  history->date[last] = inStats->date;
+  history->date[last] = strcreate (inStats->date);
 
   char name[16];
@@ -280,5 +314,5 @@
   int i;
   for (i = 0; i < N; i++) {
-    if (outstat[i].history)  { free (outstat[i].history); }
+    if (outstat[i].history)  { dmhObjectFree (outstat[i].history); }
     if (outstat[i].filename) { free (outstat[i].filename); }
   }
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeImageIDs.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeImageIDs.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeImageIDs.c	(revision 38502)
@@ -64,4 +64,9 @@
     dvo_image_match_dbs(IDmap, &outDB, &inDB);
     dvo_image_unlock (&outDB); // unlock output
+
+    gfits_db_free (&inDB);
+    gfits_db_free (&outDB);
+    dmhImageFree (history);
+    FREE (indbID);
     return TRUE;
   }
@@ -81,6 +86,9 @@
   dvo_image_unlock (&outDB); // unlock output
 
-  gfits_db_free (inDB);
-  gfits_db_free (outDB);
+  gfits_db_free (&inDB);
+  gfits_db_free (&outDB);
+
+  dmhImageFree (history);
+  FREE (indbID);
 
   return TRUE;
@@ -139,6 +147,6 @@
   dvo_image_match_dbs(IDmap, &outDB, &inDB);
 
-  gfits_db_free (inDB);
-  gfits_db_free (outDB);
+  gfits_db_free (&inDB);
+  gfits_db_free (&outDB);
 
   return TRUE;
Index: /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeUpdate.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeUpdate.c	(revision 38501)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/dvomerge/src/dvomergeUpdate.c	(revision 38502)
@@ -8,10 +8,10 @@
   char filename[256], *input, *output;
   IDmapType IDmap;
-  PhotCodeData *inputPhotcodes;
-  PhotCodeData *outputPhotcodes;
   int *secfiltMap = NULL;
   int NsecfiltInput, NsecfiltOutput;
 
   INITTIME;
+
+  dvo_image_map_init (&IDmap);
 
   CONTINUE = FALSE;
@@ -34,4 +34,7 @@
     exit (1);
   }
+
+  PhotCodeData *inputPhotcodes  = NULL;
+  PhotCodeData *outputPhotcodes = NULL;
 
   SetPhotcodeTable(NULL);
@@ -112,5 +115,4 @@
 
   int status = dvomergeUpdate_catalogs (input, output, outsky, inlist, NsecfiltInput, NsecfiltOutput, &IDmap, secfiltMap);
-  dvo_image_map_free (&IDmap);
 
   // save the output sky table copy
@@ -121,6 +123,15 @@
     exit (1);
   }
+  FREE (skyfile);
 
-  ohana_memcheck (TRUE);
+  SkyTableFree (insky);
+  SkyTableFree (outsky);
+  SkyListFree (inlist);
+
+  FREE (secfiltMap);
+  dvo_image_map_free (&IDmap);
+  if (outputPhotcodes != inputPhotcodes) { FreePhotcodeData (outputPhotcodes); }
+  FreePhotcodeData (inputPhotcodes);
+  FreePhotcodeTable ();
 
   if (!status) {
@@ -130,4 +141,4 @@
 
   MARKTIME ("SUCCESS: elapsed time %9.4f sec\n", dtime);
-  exit (0);
+  return TRUE;
 }
