From 8b7d4c2a5535e83cdd0ce227cb4bc9a222a1d183 Mon Sep 17 00:00:00 2001
From: Ian McInerney <Ian.S.McInerney@ieee.org>
Date: Mon, 3 Feb 2020 14:02:25 +0000
Subject: [PATCH] Put tool graphics back on the stack

Now that the tool framework gracefully shutdowns the tools, graphics
can be created on the stack inside a tool function.

This reverts commit f6881ce3de34c5da8f0293b93e1bd938addf40a4.
---
 pcbnew/pcb_edit_frame.cpp           |  4 ----
 pcbnew/router/length_tuner_tool.cpp | 23 ++++++++++-------------
 pcbnew/tools/edit_tool.cpp          | 23 ++++++++++-------------
 3 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp
index 9ab757bdf1..e98523d52a 100644
--- a/pcbnew/pcb_edit_frame.cpp
+++ b/pcbnew/pcb_edit_frame.cpp
@@ -509,10 +509,6 @@ void PCB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& aEvent )
         }
     }
 
-    // Be sure no tool is left activated, to avoid crash on exit,
-    // (happens with some tools)
-    GetToolManager()->DeactivateTool();
-
     // On Windows 7 / 32 bits, on OpenGL mode only, Pcbnew crashes
     // when closing this frame if a footprint was selected, and the footprint editor called
     // to edit this footprint, and when closing pcbnew if this footprint is still selected
diff --git a/pcbnew/router/length_tuner_tool.cpp b/pcbnew/router/length_tuner_tool.cpp
index 64ecb251f1..32a06775a3 100644
--- a/pcbnew/router/length_tuner_tool.cpp
+++ b/pcbnew/router/length_tuner_tool.cpp
@@ -183,14 +183,11 @@ void LENGTH_TUNER_TOOL::performTuning()
     VECTOR2I end( m_startSnapPoint );
 
     // Create an instance of PNS_TUNE_STATUS_POPUP.
-    // DO NOT create it on the stack: otherwise on Windows, wxWidgets 3.1.3
-    // it creates a crash. I am pretty sure this crash is created by the stack switching
-    // when managing events (JPC).
-    std::unique_ptr<PNS_TUNE_STATUS_POPUP> statusPopup( new PNS_TUNE_STATUS_POPUP( frame() ) );
-    statusPopup->Popup();
+    PNS_TUNE_STATUS_POPUP statusPopup( frame() );
+    statusPopup.Popup();
 
     m_router->Move( end, NULL );
-    updateStatusPopup( *statusPopup );
+    updateStatusPopup( statusPopup );
 
     while( TOOL_EVENT* evt = Wait() )
     {
@@ -202,7 +199,7 @@ void LENGTH_TUNER_TOOL::performTuning()
         {
             end = evt->Position();
             m_router->Move( end, NULL );
-            updateStatusPopup( *statusPopup );
+            updateStatusPopup( statusPopup );
         }
         else if( evt->IsClick( BUT_LEFT ) )
         {
@@ -218,32 +215,32 @@ void LENGTH_TUNER_TOOL::performTuning()
         {
             placer->AmplitudeStep( -1 );
             m_router->Move( end, NULL );
-            updateStatusPopup( *statusPopup );
+            updateStatusPopup( statusPopup );
         }
         else if( evt->IsAction( &ACT_AmplIncrease ) )
         {
             placer->AmplitudeStep( 1 );
             m_router->Move( end, NULL );
-            updateStatusPopup( *statusPopup );
+            updateStatusPopup( statusPopup );
         }
         else if(evt->IsAction( &ACT_SpacingDecrease ) )
         {
             placer->SpacingStep( -1 );
             m_router->Move( end, NULL );
-            updateStatusPopup( *statusPopup );
+            updateStatusPopup( statusPopup );
         }
         else if( evt->IsAction( &ACT_SpacingIncrease ) )
         {
             placer->SpacingStep( 1 );
             m_router->Move( end, NULL );
-            updateStatusPopup( *statusPopup );
+            updateStatusPopup( statusPopup );
         }
         else if( evt->IsAction( &ACT_Settings ) )
         {
-            statusPopup->Hide();
+            statusPopup.Hide();
             TOOL_EVENT dummy;
             meanderSettingsDialog( dummy );
-            statusPopup->Show();
+            statusPopup.Show();
         }
     }
 
diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp
index 69f3b34a63..87872e7e20 100644
--- a/pcbnew/tools/edit_tool.cpp
+++ b/pcbnew/tools/edit_tool.cpp
@@ -1363,36 +1363,33 @@ int EDIT_TOOL::EditFpInFpEditor( const TOOL_EVENT& aEvent )
 bool EDIT_TOOL::pickCopyReferencePoint( VECTOR2I& aReferencePoint )
 {
     std::string         tool = "pcbnew.InteractiveEdit.selectReferencePoint";
-    // Do not create statusPopup on the stack:
-    // it crashes pcbnew when closing the current main frame
-    // (perhaps due to stack switching in coroutines)
-    std::unique_ptr<STATUS_TEXT_POPUP> statusPopup( new STATUS_TEXT_POPUP( frame() ) );
+    STATUS_TEXT_POPUP   statusPopup( frame() );
     PCBNEW_PICKER_TOOL* picker = m_toolMgr->GetTool<PCBNEW_PICKER_TOOL>();
     OPT<VECTOR2I>       pickedPoint;
     bool                done = false;
 
-    statusPopup->SetText( _( "Select reference point for the copy..." ) );
+    statusPopup.SetText( _( "Select reference point for the copy..." ) );
 
     picker->SetClickHandler(
         [&]( const VECTOR2D& aPoint ) -> bool
         {
             pickedPoint = aPoint;
-            statusPopup->SetText( _( "Selection copied." ) );
-            statusPopup->Expire( 800 );
+            statusPopup.SetText( _( "Selection copied." ) );
+            statusPopup.Expire( 800 );
             return false;  // we don't need any more points
         } );
 
     picker->SetMotionHandler(
         [&] ( const VECTOR2D& aPos )
         {
-            statusPopup->Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
+            statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
         } );
 
     picker->SetCancelHandler(
         [&]()
         {
-            statusPopup->SetText( _( "Copy cancelled." ) );
-            statusPopup->Expire( 800 );
+            statusPopup.SetText( _( "Copy cancelled." ) );
+            statusPopup.Expire( 800 );
         } );
 
     picker->SetFinalizeHandler(
@@ -1401,8 +1398,8 @@ bool EDIT_TOOL::pickCopyReferencePoint( VECTOR2I& aReferencePoint )
             done = true;
         } );
 
-    statusPopup->Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
-    statusPopup->Popup();
+    statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
+    statusPopup.Popup();
 
     m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
 
@@ -1410,7 +1407,7 @@ bool EDIT_TOOL::pickCopyReferencePoint( VECTOR2I& aReferencePoint )
         Wait();
 
     // Ensure statusPopup is hidden after use and before deleting it:
-    statusPopup->Hide();
+    statusPopup.Hide();
 
     if( pickedPoint.is_initialized() )
         aReferencePoint = pickedPoint.get();