diff --git a/libgnucash/engine/qofsession.cpp b/libgnucash/engine/qofsession.cpp index d15752a7e1..a42ad09123 100644 --- a/libgnucash/engine/qofsession.cpp +++ b/libgnucash/engine/qofsession.cpp @@ -225,8 +225,9 @@ QofSessionImpl::load (QofPercentageFunc percentage_func) noexcept (err != ERR_SQL_DB_TOO_OLD) && (err != ERR_SQL_DB_TOO_NEW)) { - qof_book_destroy(m_book); + auto old_book = m_book; m_book = qof_book_new(); + qof_book_destroy(old_book); LEAVE ("error from backend %d", get_error ()); return; } diff --git a/libgnucash/engine/test/test-qofsession.cpp b/libgnucash/engine/test/test-qofsession.cpp index bdb448c5d2..4e7fd98445 100644 --- a/libgnucash/engine/test/test-qofsession.cpp +++ b/libgnucash/engine/test/test-qofsession.cpp @@ -170,20 +170,22 @@ TEST (QofSessionTest, clear_error) TEST (QofSessionTest, load) { - // We register a provider that gives a backend that - // throws an error on load. - // This error during load should cause the qof session to - // "roll back" the book load. + /* We register a provider that gives a backend that throws an error on load. + * This error during load should cause the qof session to destroy the book + * and create a new one. + */ qof_backend_register_provider (get_provider ()); QofSession s; s.begin ("book1", false, false, false); auto book = s.get_book (); s.load (nullptr); - EXPECT_EQ (book, s.get_book ()); + EXPECT_NE (book, s.get_book ()); - // Now we'll do the load without returning an error from the backend, - // and ensure that it's the original book and that it's not empty. + /* Now we'll do the load without returning an error from the backend, + * and ensure that it's the new book from the previous test. + */ load_error = false; + book = s.get_book(); s.load (nullptr); EXPECT_EQ (book, s.get_book ()); EXPECT_EQ (s.get_error(), ERR_BACKEND_NO_ERR);