diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 667434375f7..8e5e1dc363f 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -41,9 +41,13 @@ void UndoRedo::_discard_redo() { for (int i = current_action + 1; i < actions.size(); i++) { for (List::Element *E = actions.write[i].do_ops.front(); E; E = E->next()) { if (E->get().type == Operation::TYPE_REFERENCE) { - Object *obj = ObjectDB::get_instance(E->get().object); - if (obj) { - memdelete(obj); + if (E->get().ref.is_valid()) { + E->get().ref.unref(); + } else { + Object *obj = ObjectDB::get_instance(E->get().object); + if (obj) { + memdelete(obj); + } } } } @@ -221,9 +225,13 @@ void UndoRedo::_pop_history_tail() { for (List::Element *E = actions.write[0].undo_ops.front(); E; E = E->next()) { if (E->get().type == Operation::TYPE_REFERENCE) { - Object *obj = ObjectDB::get_instance(E->get().object); - if (obj) { - memdelete(obj); + if (E->get().ref.is_valid()) { + E->get().ref.unref(); + } else { + Object *obj = ObjectDB::get_instance(E->get().object); + if (obj) { + memdelete(obj); + } } } }