changeset 30738:3c4368f0a4ab stable

clear workspace model before exit (bug #61994) * interpreter.cc (interpreter::shutdown): Clear all variables from workspace and notify event manager before processing events and disabling event manager. Also queue event to close gui before processing events and disabling event manager.
author John W. Eaton <jwe@octave.org>
date Sat, 12 Feb 2022 16:21:46 -0500
parents dadbfe6fddd6
children e022eab3ea1b d6ca77706ab3
files libinterp/corefcn/interpreter.cc
diffstat 1 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc	Sat Feb 12 14:19:17 2022 -0500
+++ b/libinterp/corefcn/interpreter.cc	Sat Feb 12 16:21:46 2022 -0500
@@ -966,27 +966,42 @@
 
     OCTAVE_SAFE_CALL (feval, ("close", ovl ("all"), 0));
 
-    // If we are attached to a GUI, process pending events and
-    // disable the link.
-
-    OCTAVE_SAFE_CALL (m_event_manager.process_events, (true));
-    OCTAVE_SAFE_CALL (m_event_manager.disable, ());
-
-    OCTAVE_SAFE_CALL (m_input_system.clear_input_event_hooks, ());
-
     // Any atexit functions added after this function call won't be
     // executed.  Each atexit function is executed with
     // OCTAVE_SAFE_CALL, so we don't need that here.
 
     execute_atexit_fcns ();
 
-    // Clear all functions and variables.
+    // Clear all functions and variables while the event manager is
+    // still processing events and notify the event manager.  This way,
+    // the workspace model will be cleared before the GUI exits.
+
+    // FIXME: This approach seems a bit fragile since there could be
+    // other places in the GUI that have references to interpreter
+    // objects.  How can we reliably ensure that they are all removed
+    // before the interpreter exits?  Maybe the best solution is to
+    // always start the GUI from the interpreter and close it when the
+    // interpreter exits?  However, the workspace model is owned by the
+    // base_qobject object not the workspace viewer or the main window,
+    // so simply closing the GUI window(s) is not sufficient.  See also
+    // bug #61994.
 
     // Note that we don't force symbols to be cleared, so we will
     // respect mlock at this point.  Later, we'll force all variables
     // and functions to be cleared.
 
     OCTAVE_SAFE_CALL (clear_all, ());
+    OCTAVE_SAFE_CALL (m_event_manager.set_workspace, ());
+
+    // If we are attached to a GUI, queue and event to close it (only
+    // works with the new terminal widget), process pending events and
+    // disable the link.
+
+    OCTAVE_SAFE_CALL (m_event_manager.close_gui, ());
+    OCTAVE_SAFE_CALL (m_event_manager.process_events, (true));
+    OCTAVE_SAFE_CALL (m_event_manager.disable, ());
+
+    OCTAVE_SAFE_CALL (m_input_system.clear_input_event_hooks, ());
 
     // We may still have some figures.  Close them.