From 9c64c9af5dfb23a2ff4e4e229494f1815a7b043d Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 24 Mar 2020 20:47:49 +0100 Subject: [PATCH] Kicad manager: fix an issue on Windows when killing Kicad. For some obscure reason, on Windows, when killing Kicad from the Windows task manager if a editor frame (schematic, library, board editor or fp editor) is open and has some edition to save, OnCloseWindow is run twice *at the same time*, creating race conditions between OnCloseWindow() code. Fixes #4072 https://gitlab.com/kicad/code/kicad/issues/4072 --- kicad/mainframe.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kicad/mainframe.cpp b/kicad/mainframe.cpp index d6c69d0e36..c75661485d 100644 --- a/kicad/mainframe.cpp +++ b/kicad/mainframe.cpp @@ -209,6 +209,22 @@ void KICAD_MANAGER_FRAME::OnSize( wxSizeEvent& event ) void KICAD_MANAGER_FRAME::OnCloseWindow( wxCloseEvent& Event ) { +#ifdef _WINDOWS_ + // For some obscure reason, on Windows, when killing Kicad from the Windows task manager + // if a editor frame (schematic, library, board editor or fp editor) is open and has + // some edition to save, OnCloseWindow is run twice *at the same time*, creating race + // conditions between OnCloseWindow() code. + // Therefore I added (JPC) a ugly hack to discard the second call (unwanted) during + // execution of the first call (only one call is right). + // Note also if there is no change made in editors, this behavior does not happen. + static std::atomic lock_close_event( 0 ); + + if( ++lock_close_event > 1 ) // Skip extra calls + { + return; + } +#endif + if( Kiway().PlayersClose( false ) ) { int px, py; @@ -233,6 +249,10 @@ void KICAD_MANAGER_FRAME::OnCloseWindow( wxCloseEvent& Event ) Destroy(); } + +#ifdef _WINDOWS_ + lock_close_event = 0; // Reenable event management +#endif }