Index: /trunk/Ohana/src/imregister/imreg/db.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/db.c	(revision 85)
+++ /trunk/Ohana/src/imregister/imreg/db.c	(revision 86)
@@ -53,4 +53,5 @@
 
   lockstate = (output.modify || output.delete) ? LCK_HARD : LCK_SOFT;
+  header.buffer = matrix.buffer = theader.buffer = table.buffer = (char *) NULL;
 
   if (dBFile == (char *) NULL) {
@@ -61,15 +62,15 @@
   /* lock database (soft) */
   f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
+  if (dbstate == LCK_EMPTY) return (0);
   if (f == (FILE *) NULL) {
     fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
     exit (1);
   }
-  if (dbstate == LCK_EMPTY) return (0);
-
-  /* load in table data */
+
+  /* init & load in table data */
   table.header = &theader;
-  if (!fits_fread_header (f, &header))                     escape (UNLOCK, "ERROR: can't read primary header"); 
-  if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary header");
-  if (!fits_fread_ftable  (f, &table, "IMAGE_DATABASE"))   escape (UNLOCK, "ERROR: can't read primary header");
+  if (!fits_fread_header (f, &header))                  escape (UNLOCK, "ERROR: can't read primary header"); 
+  if (!fits_fread_matrix (f, &matrix, &header))         escape (UNLOCK, "ERROR: can't read primary header");
+  if (!fits_fread_ftable (f, &table, "IMAGE_DATABASE")) escape (UNLOCK, "ERROR: can't read primary header");
 
   /* convert to internal format */
@@ -94,8 +95,14 @@
   make_backup (dBFile);
   Fseek (f, 0, SEEK_SET);
-  if (!fits_fwrite_header (f, &header))   escape (LOCK, "ERROR: can't update db");
-  if (!fits_fwrite_matrix (f, &matrix))   escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_header  (f, &header))  escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_matrix  (f, &matrix))  escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
+
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  fits_free_table  (&table);
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -137,4 +144,11 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
+
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  fits_free_vtable (&vtable);
+  fits_free_table  (&table);
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -158,4 +172,9 @@
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
 
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  fits_free_vtable (&vtable);
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -177,4 +196,10 @@
 
 int close_db () {
+
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  fits_free_table  (&table);
+
   if (dbstate == LCK_UNLOCK) return (TRUE);
   fclearlockfile (dBFile, f, lockstate, &dbstate);
@@ -183,7 +208,28 @@
 
 int close_lock_db () {
-  close (f);
+
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  fits_free_table  (&table);
+
+  if (dbstate == LCK_UNLOCK) return (TRUE);
+  fclearlockfile (dBFile, f, LCK_XCLD, &dbstate);
   return (TRUE);
 }  
+
+int print_db_status (char *message) {
+
+  int fd;
+
+  if (!output.verbose) return (0);
+  if (f == (FILE *) NULL) {
+    fprintf (stderr, "%s - db: %s - fd: %d\n", message, "null", -1);
+    return (0);
+  }
+  fd = fileno (f);
+  fprintf (stderr, "%s - db: %s - fd: %d\n", message, dBFile, fd);
+  return (1);
+}
 
 int escape (int mode, char *message) {
