From 3a567a3ae1c144cef5288ccc01e554a4a1a7436f Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sun, 1 Oct 2023 17:54:17 -0400 Subject: [PATCH] Unbind event handlers in the DIALOG_SHIM destructor to prevent late event crashes Fixes KICAD-1XF --- common/dialog_shim.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/common/dialog_shim.cpp b/common/dialog_shim.cpp index 8cc6297e2a..1eb754ec03 100644 --- a/common/dialog_shim.cpp +++ b/common/dialog_shim.cpp @@ -133,12 +133,41 @@ DIALOG_SHIM::DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& titl Pgm().App().SetTopWindow( (EDA_BASE_FRAME*) kiwayHolder ); #endif - Connect( wxEVT_PAINT, wxPaintEventHandler( DIALOG_SHIM::OnPaint ) ); + Bind( wxEVT_PAINT, &DIALOG_SHIM::OnPaint, this ); } DIALOG_SHIM::~DIALOG_SHIM() { + Unbind( wxEVT_CLOSE_WINDOW, &DIALOG_SHIM::OnCloseWindow, this ); + Unbind( wxEVT_BUTTON, &DIALOG_SHIM::OnButton, this ); + Unbind( wxEVT_PAINT, &DIALOG_SHIM::OnPaint, this ); + + std::function disconnectFocusHandlers = [&]( wxWindowList& children ) + { + for( wxWindow* child : children ) + { + if( wxTextCtrl* textCtrl = dynamic_cast( child ) ) + { + textCtrl->Disconnect( wxEVT_SET_FOCUS, + wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ), nullptr, + this ); + } + else if( wxStyledTextCtrl* scintilla = dynamic_cast( child ) ) + { + scintilla->Disconnect( wxEVT_SET_FOCUS, + wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ), nullptr, + this ); + } + else + { + disconnectFocusHandlers( child->GetChildren() ); + } + } + }; + + disconnectFocusHandlers( GetChildren() ); + // if the dialog is quasi-modal, this will end its event loop if( IsQuasiModal() ) EndQuasiModal( wxID_CANCEL );