From 1544ba0edc8d1791d1d3a79bb0451831d0408051 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Fri, 7 May 2010 23:38:31 +0000
Subject: [PATCH] Fix crash in wxCommandProcessor of capacity N when N-1
 commands were undone.

Performing N commands (where N is the maximal number of commands stored by
wxCommandProcessor), undoing N-1 of them and performing another command
resulted in a crash because a dangling pointer was left.

Closes #12027.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64245 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 src/common/cmdproc.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/common/cmdproc.cpp b/src/common/cmdproc.cpp
index 789e950f95..3d3d024abf 100644
--- a/src/common/cmdproc.cpp
+++ b/src/common/cmdproc.cpp
@@ -112,18 +112,6 @@ void wxCommandProcessor::Store(wxCommand *command)
 {
     wxCHECK_RET( command, wxT("no command in wxCommandProcessor::Store") );
 
-    if ( (int)m_commands.GetCount() == m_maxNoCommands )
-    {
-        wxList::compatibility_iterator firstNode = m_commands.GetFirst();
-        wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
-        delete firstCommand;
-        m_commands.Erase(firstNode);
-
-        // Make sure m_lastSavedCommand won't point to freed memory
-        if ( m_lastSavedCommand == firstNode )
-            m_lastSavedCommand = wxList::compatibility_iterator();
-    }
-
     // Correct a bug: we must chop off the current 'branch'
     // so that we're at the end of the command list.
     if (!m_currentCommand)
@@ -145,6 +133,18 @@ void wxCommandProcessor::Store(wxCommand *command)
         }
     }
 
+    if ( (int)m_commands.GetCount() == m_maxNoCommands )
+    {
+        wxList::compatibility_iterator firstNode = m_commands.GetFirst();
+        wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
+        delete firstCommand;
+        m_commands.Erase(firstNode);
+
+        // Make sure m_lastSavedCommand won't point to freed memory
+        if ( m_lastSavedCommand == firstNode )
+            m_lastSavedCommand = wxList::compatibility_iterator();
+    }
+
     m_commands.Append(command);
     m_currentCommand = m_commands.GetLast();
     SetMenuStrings();
-- 
2.47.2