Surprising (= bad) memory management of Gtk::Window
This code illustrates that in in GTK4, but not GTK3, a Gtk::Window
can continue to exist while its underlying C instance is deleted:
#include <gtkmm.h>
struct Window : Gtk::Window
{
Gtk::Button button;
Gtk::Window dialog;
Window()
{
#if GTK_MAJOR_VERSION == 4
set_child(button);
#elif GTK_MAJOR_VERSION == 3
add(button);
#endif
button.signal_clicked().connect([this] {
dialog.present();
});
}
};
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create();
#if GTK_MAJOR_VERSION == 4
return app->make_window_and_run<Window>(argc, argv);
#elif GTK_MAJOR_VERSION == 3
Window window;
window.show_all();
return app->run(window);
#endif
}
If you press the button twice, then on the second time you get the message
Gtk-CRITICAL **: gtk_window_present_with_time: assertion 'GTK_IS_WINDOW (window)' failed
This is very surprising, because the C++ object continues to exist but is now in an invalid state where using it in any way causes a memory error.