From 5d732522fef11dc1b43d0b8bad7adbb5d5fdce3f Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 13 May 2016 12:54:41 +0200 Subject: [PATCH] wxZRColaCharGrid memory leak fixed (fixes #23) --- ZRCola/zrcolachrgrid.cpp | 23 +++++++++++------------ ZRCola/zrcolachrgrid.h | 1 + 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ZRCola/zrcolachrgrid.cpp b/ZRCola/zrcolachrgrid.cpp index d33d9f6..16555c9 100644 --- a/ZRCola/zrcolachrgrid.cpp +++ b/ZRCola/zrcolachrgrid.cpp @@ -61,23 +61,21 @@ wxZRColaCharGrid::~wxZRColaCharGrid() void wxZRColaCharGrid::Init() { - m_isResizing = false; - m_toolTipIdx = (size_t)-1; + m_regenerate = false; + m_isResizing = false; + m_toolTipIdx = (size_t)-1; } void wxZRColaCharGrid::SetCharacters(const wxString &chars) { - m_chars = chars; + m_chars = chars; + m_regenerate = true; - // Build and set new grid data. - size_t char_len = m_chars.Length(); - int rows = std::max((char_len + m_numCols - 1) / m_numCols, 1); - wxGridStringTable *table = new wxGridStringTable(rows, m_numCols); - for (int r = 0, i = 0; r < rows; r++) - for (int c = 0; c < m_numCols; c++, i++) - table->SetValue(r, c, i < char_len ? wxString(1, m_chars[i]) : wxEmptyString); - SetTable(table, true); + // Invoke OnSize(), which will populate the grid. + wxSizeEvent e(GetSize(), m_windowId); + e.SetEventObject(this); + HandleWindowEvent(e); } @@ -134,13 +132,14 @@ void wxZRColaCharGrid::OnSize(wxSizeEvent& event) BeginBatch(); - if (cols != m_numCols) { + if (m_regenerate || cols != m_numCols) { // Build and set new grid data. wxGridStringTable *table = new wxGridStringTable(rows, cols); for (int r = 0, i = 0; r < rows; r++) for (int c = 0; c < cols; c++, i++) table->SetValue(r, c, i < char_len ? wxString(1, m_chars[i]) : wxEmptyString); SetTable(table, true); + m_regenerate = false; } for (int c = 0; c < cols; c++) diff --git a/ZRCola/zrcolachrgrid.h b/ZRCola/zrcolachrgrid.h index 35713f5..bcff008 100644 --- a/ZRCola/zrcolachrgrid.h +++ b/ZRCola/zrcolachrgrid.h @@ -84,6 +84,7 @@ protected: wxString m_chars; ///< Array of Unicode characters to display in the grid private: + bool m_regenerate; ///< Force regenerate grid table bool m_isResizing; ///< Prevents nesting of OnSize() method. wxTimer m_timerToolTip; ///< Timer for displaying tooltip size_t m_toolTipIdx; ///< Index of cell for tooltip display