From 095d3d7134f5668b96cf1d70997d3f110b03c898 Mon Sep 17 00:00:00 2001 From: David Bremner Date: Tue, 14 Jul 2020 08:25:28 -0300 Subject: [PATCH] lib: move deallocation of memory from n_d_close to n_d_destroy 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 | 2 +- lib/database.cc | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/database-private.h b/lib/database-private.h index 76359007..d2c25313 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -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; diff --git a/lib/database.cc b/lib/database.cc index 2f794164..cfb19ccb 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -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;