From eb8e0bc2b536249bbd35d47e03cacf5972a1edaa Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Sun, 5 Nov 2023 22:01:09 +0100 Subject: [PATCH] Fix gtk_widget_destroy warnings gtk_remove_widget unrefs the widget and destroys it and all its children. Since we also do that in the destructor, we do a double free. To fix this, a ref is added to prevent destroying by gtk_remove_widget. --- src/Widget.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Widget.cpp b/src/Widget.cpp index 2b7fe37..c371f38 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -47,9 +47,12 @@ namespace Utils Widget::~Widget() { - m_Childs.clear(); - LOG("Destroy widget"); - gtk_widget_destroy(m_Widget); + if (m_Widget) + { + LOG("Destroy widget and its children"); + m_Childs.clear(); + gtk_widget_destroy(m_Widget); + } } void Widget::CreateAndAddWidget(Widget* widget, GtkWidget* parentWidget) @@ -144,8 +147,10 @@ void Widget::RemoveChild(Widget* widget) { if (m_Widget) { + LOG("Remove widget from parent"); + // Ref the widget, so we don't mess up the RAII cleanup below + g_object_ref(it->get()->m_Widget); gtk_container_remove((GtkContainer*)m_Widget, it->get()->m_Widget); - it->get()->m_Widget = nullptr; } m_Childs.erase(it); }