gnucash maint: Bug 797906 - GnuCash data file is locked after a "save-as"...

John Ralls jralls at code.gnucash.org
Tue Mar 23 14:27:46 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/170502c6 (commit)
	from  https://github.com/Gnucash/gnucash/commit/0c15dc17 (commit)



commit 170502c6c5c17e3138df1d4cb9669ca2803dc855
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Mar 23 09:48:19 2021 -0700

    Bug 797906 - GnuCash data file is locked after a "save-as"...
    
    to mysql database.
    
    It's more general, any save-as to a different backend type, i.e.
    xml->sql or sql->xml, left the previous book locked.
    
    Ensure that the session is ended and the book unlocked in the
    backend's destructor. This also fixes a memory leak in the SQL
    backend because the GncSqlConnection wasn't being deleted.

diff --git a/libgnucash/backend/sql/gnc-sql-backend.cpp b/libgnucash/backend/sql/gnc-sql-backend.cpp
index 7fcca7f7a..082e3403b 100644
--- a/libgnucash/backend/sql/gnc-sql-backend.cpp
+++ b/libgnucash/backend/sql/gnc-sql-backend.cpp
@@ -88,6 +88,11 @@ GncSqlBackend::GncSqlBackend(GncSqlConnection *conn, QofBook* book) :
         connect (conn);
 }
 
+GncSqlBackend::~GncSqlBackend()
+{
+    connect(nullptr);
+}
+
 void
 GncSqlBackend::connect(GncSqlConnection *conn) noexcept
 {
diff --git a/libgnucash/backend/sql/gnc-sql-backend.hpp b/libgnucash/backend/sql/gnc-sql-backend.hpp
index 4423ccb4f..cbdfdaf93 100644
--- a/libgnucash/backend/sql/gnc-sql-backend.hpp
+++ b/libgnucash/backend/sql/gnc-sql-backend.hpp
@@ -66,7 +66,7 @@ class GncSqlBackend : public QofBackend
 {
 public:
     GncSqlBackend(GncSqlConnection *conn, QofBook* book);
-    virtual ~GncSqlBackend() = default;
+    virtual ~GncSqlBackend();
     /**
      * Load the contents of an SQL database into a book.
      *
diff --git a/libgnucash/backend/sql/test/utest-gnc-backend-sql.cpp b/libgnucash/backend/sql/test/utest-gnc-backend-sql.cpp
index b7669f418..f7e639ae0 100644
--- a/libgnucash/backend/sql/test/utest-gnc-backend-sql.cpp
+++ b/libgnucash/backend/sql/test/utest-gnc-backend-sql.cpp
@@ -275,7 +275,7 @@ test_gnc_sql_commit_edit (void)
 {
     QofInstance* inst;
     guint dirty_called = 0;
-    GncMockSqlConnection conn;
+    auto conn{new GncMockSqlConnection};
     const char* msg1 =
         "[GncSqlBackend::commit()] Unknown object type 'null'\n";
     GLogLevelFlags loglevel = static_cast<decltype (loglevel)>
@@ -293,8 +293,7 @@ test_gnc_sql_commit_edit (void)
 
     qof_object_initialize ();
     auto book = qof_book_new();
-    auto sql_be = new GncMockSqlBackend
-        (&conn, book);
+    auto sql_be = new GncMockSqlBackend{conn, book};
     inst  = static_cast<decltype (inst)> (g_object_new (QOF_TYPE_INSTANCE, NULL));
     qof_instance_init_data (inst, QOF_ID_NULL, book);
     qof_book_set_dirty_cb (book, test_dirty_cb, &dirty_called);
diff --git a/libgnucash/backend/xml/gnc-xml-backend.cpp b/libgnucash/backend/xml/gnc-xml-backend.cpp
index 73c2cda4c..293fce38b 100644
--- a/libgnucash/backend/xml/gnc-xml-backend.cpp
+++ b/libgnucash/backend/xml/gnc-xml-backend.cpp
@@ -49,6 +49,11 @@ extern "C"
 #define FILE_URI_PREFIX "file://"
 static QofLogModule log_module = GNC_MOD_BACKEND;
 
+GncXmlBackend::~GncXmlBackend()
+{
+    session_end();
+};
+
 bool
 GncXmlBackend::check_path (const char* fullpath, bool create)
 {
diff --git a/libgnucash/backend/xml/gnc-xml-backend.hpp b/libgnucash/backend/xml/gnc-xml-backend.hpp
index fd84c5bbb..1e6ff76e8 100644
--- a/libgnucash/backend/xml/gnc-xml-backend.hpp
+++ b/libgnucash/backend/xml/gnc-xml-backend.hpp
@@ -34,7 +34,7 @@ public:
     GncXmlBackend operator=(const GncXmlBackend&) = delete;
     GncXmlBackend(const GncXmlBackend&&) = delete;
     GncXmlBackend operator=(const GncXmlBackend&&) = delete;
-    ~GncXmlBackend() = default;
+    ~GncXmlBackend();
     void session_begin(QofSession* session, const char* new_uri,
                        SessionOpenMode mode) override;
     void session_end() override;



Summary of changes:
 libgnucash/backend/sql/gnc-sql-backend.cpp            | 5 +++++
 libgnucash/backend/sql/gnc-sql-backend.hpp            | 2 +-
 libgnucash/backend/sql/test/utest-gnc-backend-sql.cpp | 5 ++---
 libgnucash/backend/xml/gnc-xml-backend.cpp            | 5 +++++
 libgnucash/backend/xml/gnc-xml-backend.hpp            | 2 +-
 5 files changed, 14 insertions(+), 5 deletions(-)



More information about the gnucash-changes mailing list