From ae12000063125f52e1cb94ccb4f1ccaeb980e37f Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 2 Apr 2022 14:03:45 -0700 Subject: [PATCH] core: clearing dive data should clear undo stack If we clear out our dive data, we also need to clear out the undo stack because it otherwise will refer to dives that no longer exist. The cleanest way to do that would be to do it in the same function used to clear the dive data. Which causes us to call C++ code from C code and really is a bit of a mess with a circular dependency between our libraries. But this does seem better than relying on people to remember to call into a second function after clearing the data. Suggested-by: Michael Andreen Suggested-by: Berthold Stoeger Signed-off-by: Dirk Hohndel --- commands/command_base.cpp | 11 ++++++++++- core/divelist.c | 2 ++ tests/CMakeLists.txt | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/commands/command_base.cpp b/commands/command_base.cpp index 26855f0e6..e264f2f1e 100644 --- a/commands/command_base.cpp +++ b/commands/command_base.cpp @@ -23,7 +23,10 @@ void init() void clear() { - undoStack->clear(); + // this can get called from C code even if the Command code was never initialized + // (really only in the case of tests like TestParse) + if (undoStack) + undoStack->clear(); } void setClean() @@ -113,3 +116,9 @@ bool placingCommand() } } // namespace Command + +extern "C" { +void command_clear_from_C() { + Command::clear(); +} +} diff --git a/core/divelist.c b/core/divelist.c index 09e3f372a..ed38eb4f7 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -21,6 +21,7 @@ #include "trip.h" bool autogroup = false; +extern void command_clear_from_C(); void set_autogroup(bool value) { @@ -1391,6 +1392,7 @@ void clear_dive_file_data() clear_git_id(); reset_tank_info_table(&tank_info_table); + command_clear_from_C(); /* Inform frontend of reset data. This should reset all the models. */ emit_reset_signal(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index feb385dec..5a3592607 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -64,6 +64,8 @@ function(TEST NAME FILE) subsurface_backend_shared ${TEST_SPECIFIC_LIBRARIES} subsurface_corelib + subsurface_commands + subsurface_corelib # annoying circular references RESOURCE_LIBRARY ${QT_TEST_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES} @@ -99,6 +101,8 @@ add_executable(TestQML testqml.cpp) target_link_libraries( TestQML subsurface_corelib + subsurface_commands + subsurface_corelib # annoying circular references RESOURCE_LIBRARY ${QT_TEST_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES}