From 4a5b897d8918c1a22ed0da5b6be1daf5a6b41b50 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 20 Apr 2023 20:08:38 -0700 Subject: [PATCH] Bug 798863 - Crash on clicking Settings button second time Ensure that dialog resources stored in options are freed when the dialog is destroyed. The crash happened when a new dialog replaced the old one on the options and the old one's destructors tried to access a dangling reference to a GtkWidget. --- gnucash/gnome-utils/dialog-options.cpp | 7 +++++++ libgnucash/engine/gnc-option.cpp | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/gnucash/gnome-utils/dialog-options.cpp b/gnucash/gnome-utils/dialog-options.cpp index af93d9822a9..16e51920da2 100644 --- a/gnucash/gnome-utils/dialog-options.cpp +++ b/gnucash/gnome-utils/dialog-options.cpp @@ -629,6 +629,13 @@ GncOptionsDialog::~GncOptionsDialog() gnc_unregister_gui_component_by_data(m_component_class, this); g_signal_handlers_disconnect_by_func(m_window, (gpointer)dialog_destroy_cb, this); g_signal_handlers_disconnect_by_func(m_window, (gpointer)dialog_window_key_press_cb, this); + m_option_db->foreach_section([](GncOptionSectionPtr& section) + { + section->foreach_option([](GncOption& option) + { + option.set_ui_item(std::unique_ptr(nullptr)); + }); + }); g_object_unref(m_window); } diff --git a/libgnucash/engine/gnc-option.cpp b/libgnucash/engine/gnc-option.cpp index f43fd778d34..49fa74e73a9 100644 --- a/libgnucash/engine/gnc-option.cpp +++ b/libgnucash/engine/gnc-option.cpp @@ -221,7 +221,8 @@ GncOption::set_ui_item(GncOptionUIItemPtr&& ui_item) return option.get_ui_type(); }, *m_option); - if (ui_item->get_ui_type() != opt_ui_type) + //ui_item may be nullptr to free the old m_ui_item. + if (ui_item && ui_item->get_ui_type() != opt_ui_type) { PERR("Setting option %s:%s UI element failed, mismatched UI types.", get_section().c_str(), get_name().c_str());