GTK: Fix crash when deleting wxTextCtrl in key handler (PR #26006)

19 views
Skip to first unread message

RobertRoeb

unread,
Dec 1, 2025, 3:59:22 AM (2 days ago) Dec 1
to wx-...@googlegroups.com, Subscribed
  • Key handler collects events and sends a message in an "event-after" signal. This event arrives after deletion leading to a crash.
  • Maybe there is a way to disconnect from that signal, but the current flags prevents the crash

You can view, comment on, or merge this pull request online at:

  https://github.com/wxWidgets/wxWidgets/pull/26006

Commit Summary

  • dd2efd5 Fix crash when deleting wxTextCtrl in key handler

File Changes

(3 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26006@github.com>

paulcor

unread,
Dec 1, 2025, 9:09:08 AM (2 days ago) Dec 1
to wx-...@googlegroups.com, Subscribed
paulcor left a comment (wxWidgets/wxWidgets#26006)

It would be appropriate to have a description here of how the crash can be reproduced. Maybe a stack trace too.

How about we do it like this instead?

diff --git a/src/gtk/textentry.cpp b/src/gtk/textentry.cpp
index 200d2e727f..b3d8f00cf8 100644
--- a/src/gtk/textentry.cpp
+++ b/src/gtk/textentry.cpp
@@ -43,6 +43,7 @@ class wxTextCoalesceData
 public:
     wxTextCoalesceData(GtkWidget* widget, gulong handlerAfterKeyPress)
         : m_handlerAfterKeyPress(handlerAfterKeyPress)
+        , m_widget(widget)
     {
         m_inKeyPress = false;
         m_pendingTextChanged = false;
@@ -52,12 +53,17 @@ public:
         g_signal_handler_block(widget, m_handlerAfterKeyPress);
     }
 
-    void StartHandlingKeyPress(GtkWidget* widget)
+    ~wxTextCoalesceData()
+    {
+        g_signal_handler_disconnect(m_widget, m_handlerAfterKeyPress);
+    }
+
+    void StartHandlingKeyPress()
     {
         m_inKeyPress = true;
         m_pendingTextChanged = false;
 
-        g_signal_handler_unblock(widget, m_handlerAfterKeyPress);
+        g_signal_handler_unblock(m_widget, m_handlerAfterKeyPress);
     }
 
     bool SetPendingIfInKeyPress()
@@ -70,9 +76,9 @@ public:
         return true;
     }
 
-    bool EndHandlingKeyPressAndCheckIfPending(GtkWidget* widget)
+    bool EndHandlingKeyPressAndCheckIfPending()
     {
-        g_signal_handler_block(widget, m_handlerAfterKeyPress);
+        g_signal_handler_block(m_widget, m_handlerAfterKeyPress);
 
         wxASSERT( m_inKeyPress );
         m_inKeyPress = false;
@@ -89,6 +95,7 @@ private:
     bool m_inKeyPress;
     bool m_pendingTextChanged;
     const gulong m_handlerAfterKeyPress;
+    GtkWidget* const m_widget;
 
     wxDECLARE_NO_COPY_CLASS(wxTextCoalesceData);
 };
@@ -120,14 +127,14 @@ extern "C" {
 // to send a single wxEVT_TEXT even if we received several (typically two, when
 // the selected text in the control is replaced by new text) "changed" signals.
 static gboolean
-wx_gtk_text_after_key_press(GtkWidget* widget,
+wx_gtk_text_after_key_press(GtkWidget*,
                             GdkEventKey* WXUNUSED(gdk_event),
                             wxTextEntry* entry)
 {
     wxTextCoalesceData* const data = entry->GTKGetCoalesceData();
     wxCHECK_MSG( data, FALSE, "must be non-null if this handler is called" );
 
-    if ( data->EndHandlingKeyPressAndCheckIfPending(widget) )
+    if ( data->EndHandlingKeyPressAndCheckIfPending() )
     {
         entry->GTKOnTextChanged();
     }
@@ -976,7 +983,7 @@ void wxTextEntry::GTKEntryOnKeypress(GtkWidget* widget) const
         m_coalesceData = new wxTextCoalesceData(widget, handler);
     }
 
-    m_coalesceData->StartHandlingKeyPress(widget);
+    m_coalesceData->StartHandlingKeyPress();
 }
 
 int wxTextEntry::GTKEntryIMFilterKeypress(GdkEventKey* event) const


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26006/c3596741255@github.com>

RobertRoeb

unread,
Dec 1, 2025, 11:52:25 AM (2 days ago) Dec 1
to wx-...@googlegroups.com, Subscribed
RobertRoeb left a comment (wxWidgets/wxWidgets#26006)

Thanks, that one works as well - and looks as if it would not break backward comp, so it could be backported.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26006/c3597698416@github.com>

RobertRoeb

unread,
Dec 1, 2025, 3:24:48 PM (2 days ago) Dec 1
to wx-...@googlegroups.com, Push

@RobertRoeb pushed 1 commit.

  • 49088fe Applied Paul C's better patch to prevent GTK wxTextCtrl crash


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26006/before/dd2efd521353cfc18e2c38ee2737f6b6e83b6ad8/after/49088fe3d75525b97398db9dd9e45daefd490f91@github.com>

RobertRoeb

unread,
Dec 1, 2025, 3:25:33 PM (2 days ago) Dec 1
to wx-...@googlegroups.com, Subscribed
RobertRoeb left a comment (wxWidgets/wxWidgets#26006)

I have applied your patch and added a test to the text sample


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26006/c3598688444@github.com>

Reply all
Reply to author
Forward
0 new messages