From ce087143165a0306b8c51eccaf08bfdd118d0e92 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 25 Jun 2024 00:05:50 +0200 Subject: Fix assertion failure after removing content. Adding, say, 10 items of content, removing the last 8 and then clicking somewhere in the content list would cause OnGetItem{Text,Attr} to be called with a high index (as if the items had not yet been removed). --- src/wx/content_panel.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/wx/content_panel.cc b/src/wx/content_panel.cc index 07a632b4a..5624e8797 100644 --- a/src/wx/content_panel.cc +++ b/src/wx/content_panel.cc @@ -211,26 +211,35 @@ public: wxString OnGetItemText(long item, long) const override { - DCPOMATIC_ASSERT(item >= 0 && item < static_cast(_items.size())); + if (out_of_range(item)) { + /* wxWidgets sometimes asks for things that are already gone */ + return {}; + } wxClientDC dc(const_cast(static_cast(this))); return wxControl::Ellipsize(_items[item].text, dc, wxELLIPSIZE_MIDDLE, GetSize().GetWidth()); } wxListItemAttr* OnGetItemAttr(long item) const override { - DCPOMATIC_ASSERT(item >= 0 && item < static_cast(_items.size())); + if (out_of_range(item)) { + return nullptr; + } return _items[item].error ? const_cast(&_red) : nullptr; } weak_ptr content_at_index(long index) { - if (index < 0 || index >= static_cast(_items.size())) { + if (out_of_range(index)) { return {}; } return _items[index].content; } private: + bool out_of_range(long index) const { + return index < 0 || index >= static_cast(_items.size()); + } + std::vector _items; wxListItemAttr _red; }; -- cgit v1.2.3