Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
reduce undo/redo memory leak
  • Loading branch information
wschweer committed Nov 6, 2014
1 parent 5ed2bc8 commit 3f99702
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
46 changes: 45 additions & 1 deletion libmscore/undo.cpp
Expand Up @@ -116,10 +116,20 @@ void updateNoteLines(Segment* segment, int track)

UndoCommand::~UndoCommand()
{
foreach(UndoCommand* c, childList)
for (auto c : childList)
delete c;
}

//---------------------------------------------------------
// UndoCommand::cleanup
//---------------------------------------------------------

void UndoCommand::cleanup(bool undo)
{
for (auto c : childList)
c->cleanup(undo);
}

//---------------------------------------------------------
// undo
//---------------------------------------------------------
Expand Down Expand Up @@ -182,6 +192,12 @@ UndoStack::UndoStack()

UndoStack::~UndoStack()
{
int idx;
for (auto c : list) {
printf("cleanup %d <= %d\n", idx, curIdx);
c->cleanup(idx++ < curIdx);
}
qDeleteAll(list);
}

//---------------------------------------------------------
Expand Down Expand Up @@ -214,8 +230,10 @@ void UndoStack::endMacro(bool rollback)
if (rollback)
delete curCmd;
else {
// remove redo stack
while (list.size() > curIdx) {
UndoCommand* cmd = list.takeLast();
cmd->cleanup(false); // delete elements for which UndoCommand() holds ownership
delete cmd;
}
list.append(curCmd);
Expand Down Expand Up @@ -1387,6 +1405,19 @@ AddElement::AddElement(Element* e)
element = e;
}

//---------------------------------------------------------
// AddElement::cleanup
//---------------------------------------------------------

void AddElement::cleanup(bool undo)
{
if (!undo) {
qDebug("delete %d %s", undo, element->name());
delete element;
element = 0;
}
}

//---------------------------------------------------------
// undoRemoveTuplet
//---------------------------------------------------------
Expand Down Expand Up @@ -1552,6 +1583,19 @@ RemoveElement::RemoveElement(Element* e)
}
}

//---------------------------------------------------------
// AddElement::cleanup
//---------------------------------------------------------

void RemoveElement::cleanup(bool undo)
{
if (undo) {
qDebug("delete %d %s", undo, element->name());
delete element;
element = 0;
}
}

//---------------------------------------------------------
// undo
//---------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions libmscore/undo.h
Expand Up @@ -105,6 +105,7 @@ class UndoCommand {
UndoCommand* removeChild() { return childList.takeLast(); }
int childCount() const { return childList.size(); }
void unwind();
virtual void cleanup(bool undo);
#ifdef DEBUG_UNDO
virtual const char* name() const { return "UndoCommand"; }
#endif
Expand Down Expand Up @@ -612,6 +613,7 @@ class AddElement : public UndoCommand {
AddElement(Element*);
virtual void undo();
virtual void redo();
virtual void cleanup(bool);
#ifdef DEBUG_UNDO
virtual const char* name() const;
#endif
Expand All @@ -628,6 +630,7 @@ class RemoveElement : public UndoCommand {
RemoveElement(Element*);
virtual void undo();
virtual void redo();
virtual void cleanup(bool);
#ifdef DEBUG_UNDO
virtual const char* name() const;
#endif
Expand Down

0 comments on commit 3f99702

Please sign in to comment.