Ticket #15540 (closed defect: fixed)
wxRichTextCtrlTable: crashes due to an invalid focus object
|Reported by:||dghart||Owned by:|
|Keywords:||wxRichTextTable focus crash||Cc:|
#15189 fixed crashes from the wxRICHTEXT_CHANGE_OBJECT section of wxRichTextAction::Do when the focused cell was deleted. However I've now found different ways to cause similar crashes. This can be seen in the richtext sample once the patch from #15539 is applied.
Run the sample and click in one of the table's cells. Use the menu to add a new column. Undo it, then type a letter. The sample will assert and crash.
In this instance the problem is that 'container', a richtextcell, is invalid in wxRichTextAction::Do, called by wxRichTextParagraphLayoutBox::InsertTextWithUndo. However the underlying issue is that, whenever a cell has focus, that focus will always become invalid during or after either Undo or Redo, as these swap table instances. Though the details varied with different attempts at solving this, crashes always happened, mostly during invalidation or painting.
Giving the table focus isn't a solution: doing so actually sets focus to cell(0,0). Nor can it be solved by setting the focus elsewhere in the calling code: this works fine at first, but if afterwards the user selects a cell and then does Undo/Redo... The only viable fix that I've found is to set the focus outside the table each time Undo/Redo is called. This results in the caret being located immediately after the table, which is OK if a cell did have focus, and I feel is acceptable even if the focus was elswhere.
richtextbuffer.patch implements this. and reverts the #15189 partial fix.