[gtkmm] Gtk::Application::make_window_and_run(): Delay the deletion



commit dfb908d1cdb52caa0a70b02518a81c8ed8701afc
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Mar 13 18:53:35 2022 +0100

    Gtk::Application::make_window_and_run(): Delay the deletion
    
    Let Gtk::Object::destroy_notify_() delete the created C++ wrapper
    (a Gtk::Window or a descendant of Gtk::Window) when the underlying
    C instance is destroyed. Signal handlers such as on_unrealize()
    are then called before the C++ wrapper is deleted. Fixes #114

 gtk/src/application.hg | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
---
diff --git a/gtk/src/application.hg b/gtk/src/application.hg
index 1d1f7c6c..afb723da 100644
--- a/gtk/src/application.hg
+++ b/gtk/src/application.hg
@@ -297,7 +297,16 @@ int Application::make_window_and_run(int argc, char** argv, T_Args&&... args)
   static_assert(std::is_base_of<Window, T_Window>::value);
   
   signal_activate().connect([this, &args...] () {
+    // The created window is managed. Thus, the C++ wrapper is deleted
+    // by Gtk::Object::destroy_notify_() when the window is destroyed.
+    // https://gitlab.gnome.org/GNOME/gtkmm/-/issues/114
+    //TODO: Simplify when Gtk::Window::set_manage() can make a Gtk::Window managed.
+    //auto window = make_managed<T_Window>(std::forward<T_Args>(args)...);
     auto window = new T_Window(std::forward<T_Args>(args)...);
+    bool was_floating = g_object_is_floating(window->gobj());
+    window->Gtk::Object::set_manage();
+    if (!was_floating && g_object_is_floating(window->gobj()))
+      g_object_ref_sink(window->gobj());
     m_run_window = window;
     add_window(*window);
     window->show();
@@ -306,7 +315,8 @@ int Application::make_window_and_run(int argc, char** argv, T_Args&&... args)
   signal_window_removed().connect([this] (Window* window) {
     if (window == m_run_window)
     {
-      delete window;
+      if (window && window->gobj())
+        gtk_window_destroy(window->gobj());
       m_run_window = nullptr;
     }
   });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]