TextEditorWidget: Prolong the lifetime of old document

The crash happens when calling TextEditorWidget::setTextDocument()
second time for the same editor instance.

Change-Id: I0a2febb50702673e2751a0d41fc0bc80cb6ba4a2
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2022-09-22 15:18:22 +02:00
parent 4ea8823f45
commit 7b77085652

View File

@@ -1120,8 +1120,18 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
m_searchResultOverlay = new TextEditorOverlay(q);
m_refactorOverlay = new RefactorOverlay(q);
m_document = doc;
setupDocumentSignals();
{
// QPlainTextEdit keeps pointer to the old QTextDocumentLayout,
// and QPlainTextEdit::setDocument() disconnects unconditionally from the old layout.
// Since the old layout it being deleted together with the old text document when
// dropping the shared pointer reference, QPlainTextEdit::setDocument() will crash.
// We prolong the lifetime of the old document and its layout by keeping the
// shared pointer still for a while.
QSharedPointer<TextDocument> lifetimeProlonger = m_document;
m_document = doc;
setupDocumentSignals();
}
m_blockCount = doc->document()->blockCount();
// from RESEARCH