From 11fa4b939d176da02580e4a7f397bdd7c7179e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pasindu=20Gunasekara=20=F0=9F=8D=A3?= Date: Wed, 9 Feb 2022 21:29:20 +0000 Subject: [PATCH] Merged PR 648568: Detours - Avoid allocating memory in PathTree destructor - Avoids allocating wstring and vectors in the destructor Related work items: #1913787 --- .../Sandbox/Windows/DetoursServices/PathTree.cpp | 16 +++++++++++++--- .../Sandbox/Windows/DetoursServices/PathTree.h | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Public/Src/Sandbox/Windows/DetoursServices/PathTree.cpp b/Public/Src/Sandbox/Windows/DetoursServices/PathTree.cpp index 20084092e..3a871e983 100644 --- a/Public/Src/Sandbox/Windows/DetoursServices/PathTree.cpp +++ b/Public/Src/Sandbox/Windows/DetoursServices/PathTree.cpp @@ -23,9 +23,7 @@ PathTree::PathTree() PathTree::~PathTree() { - std::vector descendants; - RetrieveAndRemoveAllDescendants(L"", m_root, descendants); - + RemoveAllDescendants(m_root); delete m_root; } @@ -159,6 +157,18 @@ void PathTree::RetrieveAndRemoveAllDescendants(const std::wstring& path, TreeNod node->children.clear(); } +void PathTree::RemoveAllDescendants(TreeNode* node) +{ + for (auto iter = node->children.begin(); iter != node->children.end(); iter++) + { + RemoveAllDescendants(iter->second); + + delete iter->second; + } + + node->children.clear(); +} + bool PathTree::TryFind(const std::wstring& path, std::vector>& nodeTrace) { std::vector elements; diff --git a/Public/Src/Sandbox/Windows/DetoursServices/PathTree.h b/Public/Src/Sandbox/Windows/DetoursServices/PathTree.h index 788f1213f..82f0442fc 100644 --- a/Public/Src/Sandbox/Windows/DetoursServices/PathTree.h +++ b/Public/Src/Sandbox/Windows/DetoursServices/PathTree.h @@ -56,6 +56,9 @@ private: // Removes all descendants from the given node and builds the descendants collection using the given path as a prefix void RetrieveAndRemoveAllDescendants(const std::wstring& path, TreeNode* lastNode, std::vector& descendants); + // Removes all descendants from the given node + void RemoveAllDescendants(TreeNode* node); + // Debugging facility std::wstring ToDebugString(TreeNode* node = nullptr, std::wstring ident = L"");