From 2dbe5c72d8e9b969c6d669d0801753f82d1d693f Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 19 Nov 2021 06:34:26 +0100 Subject: [PATCH] TextEditor: fix crash after removing hover handler If we remove a hover handler from the editor we have to reset the last cached hover result in the runner to prevent using this removed and potentially deleted hover handler. Change-Id: I2bbdbee9d018aac426832552d036926be9ff198e Reviewed-by: Eike Ziller --- src/plugins/texteditor/texteditor.cpp | 39 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 499897522db..ef6e1698134 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -325,25 +325,17 @@ public: if (isCheckRunning(documentRevision, position)) return; - abortHandlers(); - // Update invocation data m_documentRevision = documentRevision; m_position = position; m_callback = callback; - // Re-initialize process data - m_currentHandlerIndex = 0; - m_bestHandler = nullptr; - m_highestHandlerPriority = -1; - - // Start checking - checkNext(); + restart(); } bool isCheckRunning(int documentRevision, int position) const { - return m_currentHandlerIndex <= m_handlers.size() + return m_currentHandlerIndex >= 0 && m_documentRevision == documentRevision && m_position == position; } @@ -376,6 +368,7 @@ public: checkNext(); return; } + m_currentHandlerIndex = -1; // All were queried, run the best if (m_bestHandler) { @@ -384,11 +377,36 @@ public: } } + void handlerRemoved(BaseHoverHandler *handler) + { + if (m_lastHandlerInfo.handler == handler) + m_lastHandlerInfo = LastHandlerInfo(); + if (m_currentHandlerIndex >= 0) + restart(); + } + private: void abortHandlers() { for (BaseHoverHandler *handler : m_handlers) handler->abort(); + m_currentHandlerIndex = -1; + } + + void restart() + { + abortHandlers(); + + if (m_handlers.empty()) + return; + + // Re-initialize process data + m_currentHandlerIndex = 0; + m_bestHandler = nullptr; + m_highestHandlerPriority = -1; + + // Start checking + checkNext(); } TextEditorWidget *m_widget; @@ -5457,6 +5475,7 @@ void TextEditorWidget::addHoverHandler(BaseHoverHandler *handler) void TextEditorWidget::removeHoverHandler(BaseHoverHandler *handler) { d->m_hoverHandlers.removeAll(handler); + d->m_hoverHandlerRunner.handlerRemoved(handler); } #ifdef WITH_TESTS