]> git.notmuchmail.org Git - notmuch/commitdiff
lib: move deallocation of memory from n_d_close to n_d_destroy
authorDavid Bremner <david@tethera.net>
Tue, 14 Jul 2020 11:25:28 +0000 (08:25 -0300)
committerDavid Bremner <david@tethera.net>
Wed, 22 Jul 2020 22:52:55 +0000 (19:52 -0300)
In order to mimic the "best effort" API of Xapian to provide
information from a closed database when possible, do not
destroy the Xapian database object too early.

Because the pointer to a Xapian database is no longer nulled on close,
introduce a flag to track whether the notmuch database is open or not.

lib/database-private.h
lib/database.cc

index 763590079f129c9bf84f363ead8c2cb150fcbdce..d2c253136d146db6cca03f2ed2b220526ab07cfb 100644 (file)
@@ -194,7 +194,7 @@ struct _notmuch_database {
     /* true if changes have been made in this atomic section */
     bool atomic_dirty;
     Xapian::Database *xapian_db;
-
+    bool open;
     /* Bit mask of features used by this database.  This is a
      * bitwise-OR of NOTMUCH_FEATURE_* values (above). */
     enum _notmuch_features features;
index 2f794164103a5f5ad14197b220c4d13d734d50c5..cfb19ccb22c3752f270cd75e08b1b3d8ce97d93f 100644 (file)
@@ -1076,6 +1076,10 @@ notmuch_database_open_verbose (const char *path,
        *database = notmuch;
     else
        talloc_free (notmuch);
+
+    if (notmuch)
+       notmuch->open = true;
+
     return status;
 }
 
@@ -1087,7 +1091,7 @@ notmuch_database_close (notmuch_database_t *notmuch)
     /* Many Xapian objects (and thus notmuch objects) hold references to
      * the database, so merely deleting the database may not suffice to
      * close it.  Thus, we explicitly close it here. */
-    if (notmuch->xapian_db != NULL) {
+    if (notmuch->open) {
        try {
            /* If there's an outstanding transaction, it's unclear if
             * closing the Xapian database commits everything up to
@@ -1110,20 +1114,7 @@ notmuch_database_close (notmuch_database_t *notmuch)
            }
        }
     }
-
-    delete notmuch->term_gen;
-    notmuch->term_gen = NULL;
-    delete notmuch->query_parser;
-    notmuch->query_parser = NULL;
-    delete notmuch->xapian_db;
-    notmuch->xapian_db = NULL;
-    delete notmuch->value_range_processor;
-    notmuch->value_range_processor = NULL;
-    delete notmuch->date_range_processor;
-    notmuch->date_range_processor = NULL;
-    delete notmuch->last_mod_range_processor;
-    notmuch->last_mod_range_processor = NULL;
-
+    notmuch->open = false;
     return status;
 }
 
@@ -1336,6 +1327,20 @@ notmuch_database_destroy (notmuch_database_t *notmuch)
     notmuch_status_t status;
 
     status = notmuch_database_close (notmuch);
+
+    delete notmuch->term_gen;
+    notmuch->term_gen = NULL;
+    delete notmuch->query_parser;
+    notmuch->query_parser = NULL;
+    delete notmuch->xapian_db;
+    notmuch->xapian_db = NULL;
+    delete notmuch->value_range_processor;
+    notmuch->value_range_processor = NULL;
+    delete notmuch->date_range_processor;
+    notmuch->date_range_processor = NULL;
+    delete notmuch->last_mod_range_processor;
+    notmuch->last_mod_range_processor = NULL;
+
     talloc_free (notmuch);
 
     return status;