From ff5981230a1adc46c4ab3d895e2ff5d0c4cf96f0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 22 Jun 2016 01:11:20 +0200 Subject: [PATCH] Fix crash when resetting wxGrid table with editor control shown This resulted in a crash in GetDefaultEditorForCell() later when GetEditor() was called from HideCellEditControl() which is itself always called from wxGrid dtor because GetDefaultEditorForCell() dereferenced m_table without checking if it was not null any more. Add the missing check to this function and GetDefaultRendererForCell() too, for consistency. In addition, dismiss the cell editor immediately when changing table instead of doing it at some later time, as it just doesn't make sense to continue showing it any more as the data it was started to edit doesn't belong to us any longer. --- src/generic/grid.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 921780caf3..594aea10bc 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -2322,6 +2322,17 @@ wxGrid::SetTable(wxGridTableBase *table, if (m_table) { + // We can't leave the in-place control editing the data of the + // table alive, as it would try to use the table object that we + // don't have any more later otherwise, so hide it manually. + // + // Notice that we can't call DisableCellEditControl() from here + // which would try to save the current editor value into the table + // which might be half-deleted by now, so we have to manually mark + // the edit control as being disabled. + HideCellEditControl(); + m_cellEditCtrlEnabled = false; + m_table->SetView(0); if( m_ownTable ) delete m_table; @@ -7933,12 +7944,18 @@ void wxGrid::RegisterDataType(const wxString& typeName, wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const { + if ( !m_table ) + return NULL; + wxString typeName = m_table->GetTypeName(row, col); return GetDefaultEditorForType(typeName); } wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const { + if ( !m_table ) + return NULL; + wxString typeName = m_table->GetTypeName(row, col); return GetDefaultRendererForType(typeName); }