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.
This commit is contained in:
David Bremner 2020-07-14 08:25:28 -03:00
parent 920dc56e60
commit 095d3d7134
2 changed files with 21 additions and 16 deletions

View 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;

View 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;