Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid memory leaks in MemWatchModel::clearRoot(). #142

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions Source/GUI/MemWatcher/MemWatchModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ void MemWatchModel::editEntry(MemWatchEntry* entry, const QModelIndex& index)

void MemWatchModel::clearRoot()
{
m_rootNode->clearAllChild();
emit layoutChanged();
beginResetModel();
m_rootNode->deleteChildren();
endResetModel();
}

void MemWatchModel::removeNode(const QModelIndex& index)
Expand Down
6 changes: 3 additions & 3 deletions Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ void MemWatchWidget::copySelectedWatchesToClipBoard()
}
rootNodeCopy.writeToJson(jsonNode);

// Clear borrowed children before going out of scope.
rootNodeCopy.clearAllChild();
// Remove borrowed children before going out of scope.
rootNodeCopy.removeChildren();
for (auto& [childNode, parentNode] : parentMap)
{
childNode->setParent(parentNode);
Expand All @@ -353,7 +353,7 @@ void MemWatchWidget::pasteWatchFromClipBoard(const QModelIndex& referenceIndex)
}

const QVector<MemWatchTreeNode*> children{copiedRootNode.getChildren()};
copiedRootNode.clearAllChild();
copiedRootNode.removeChildren();

std::vector<MemWatchTreeNode*> childrenVec(children.constBegin(), children.constEnd());
m_watchModel->addNodes(childrenVec, referenceIndex);
Expand Down
12 changes: 9 additions & 3 deletions Source/MemoryWatch/MemWatchTreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ MemWatchTreeNode::MemWatchTreeNode(MemWatchEntry* const entry, MemWatchTreeNode*

MemWatchTreeNode::~MemWatchTreeNode()
{
if (hasChildren())
qDeleteAll(m_children);
deleteChildren();
m_parent = nullptr;
delete m_entry;
m_entry = nullptr;
}

bool MemWatchTreeNode::isGroup() const
Expand Down Expand Up @@ -112,11 +112,17 @@ void MemWatchTreeNode::removeChild(const int row)
m_children.remove(row);
}

void MemWatchTreeNode::clearAllChild()
void MemWatchTreeNode::removeChildren()
{
m_children.clear();
}

void MemWatchTreeNode::deleteChildren()
{
qDeleteAll(m_children);
m_children.clear();
}

void MemWatchTreeNode::readFromJson(const QJsonObject& json, MemWatchTreeNode* parent)
{
m_parent = parent;
Expand Down
3 changes: 2 additions & 1 deletion Source/MemoryWatch/MemWatchTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class MemWatchTreeNode
void appendChild(MemWatchTreeNode* node);
void insertChild(int row, MemWatchTreeNode* node);
void removeChild(int row);
void clearAllChild();
void removeChildren();
void deleteChildren();

void readFromJson(const QJsonObject& json, MemWatchTreeNode* parent = nullptr);
void writeToJson(QJsonObject& json) const;
Expand Down
Loading