Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/engraving/editing/textedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@ void TextBase::endEdit(EditData& ed)
}

const bool newlyAdded = ted->oldXmlText.isEmpty();
bool rollbackWillDeleteThis = false;
if (newlyAdded) {
const UndoableTransaction* transaction = textWasEdited ? undo->prev() : undo->last();
if (transaction && transaction->hasCommandsMatchingFilter(UndoableCommandFilter::AddElement, this)) {
// We have just added this element to a score.
// Combine undo records of text creation with text editing.
undo->mergeTransactions(ted->startUndoIdx - 1);
// The merged transaction now contains the "add element" command for `this`,
// so rolling it back below will synchronously delete `this` (see AddElement::cleanup()).
rollbackWillDeleteThis = true;
}
}

Expand All @@ -184,6 +188,14 @@ void TextBase::endEdit(EditData& ed)
undo->reopen();
if (newlyAdded) {
score()->endCmd(true); // rollback the "add element" command

if (rollbackWillDeleteThis) {
// `this` (and therefore `ted`, which points to it) was just deleted by the
// rollback above. Any further use of them would be a use-after-free, so bail
// out immediately instead of falling through to the code below.
return;
}

ted->deleteText = true;
} else {
score()->undoRemoveElement(this);
Expand Down
Loading