diff --git a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_statement_wrapper.cpp b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_statement_wrapper.cpp index 7b1225683..5d63cc37e 100644 --- a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_statement_wrapper.cpp +++ b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_statement_wrapper.cpp @@ -60,7 +60,7 @@ std::shared_ptr SqliteStatementWrapper::execute_and_rese std::stringstream errmsg; errmsg << "Error when processing SQL statement. SQLite error (" << return_code << "): " << sqlite3_errstr(return_code); - + reset(); throw SqliteException{errmsg.str(), return_code}; } @@ -72,7 +72,7 @@ std::shared_ptr SqliteStatementWrapper::execute_and_rese std::stringstream errmsg; errmsg << "Statement returned empty value while result was expected: \'" << sqlite3_sql(statement_) << "\'"; - + reset(); throw SqliteException{errmsg.str(), return_code}; } } diff --git a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_wrapper.cpp b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_wrapper.cpp index 9e1702c1d..7f58fa003 100644 --- a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_wrapper.cpp +++ b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_wrapper.cpp @@ -85,10 +85,19 @@ SqliteWrapper::SqliteWrapper( throw SqliteException{errmsg.str()}; } } - - apply_pragma_settings(pragmas, io_flag); - sqlite3_extended_result_codes(db_ptr, 1); - initialize_application_functions(); + try { + apply_pragma_settings(pragmas, io_flag); + sqlite3_extended_result_codes(db_ptr, 1); + initialize_application_functions(); + } catch (...) { + const int rc = sqlite3_close(db_ptr); + if (rc != SQLITE_OK) { + ROSBAG2_STORAGE_DEFAULT_PLUGINS_LOG_ERROR_STREAM( + "Could not close open database. Error code: " << rc << + " Error message: " << sqlite3_errstr(rc)); + } + throw; + } } SqliteWrapper::SqliteWrapper()