From 6f3a37cdf9a79ede1ef9b6452d1accd1a1ffaa83 Mon Sep 17 00:00:00 2001 From: ssupraja777 Date: Sat, 5 Oct 2024 07:48:36 +0530 Subject: [PATCH 1/2] Remove memory leaks : 75 ssupraja777 --- src/controller/DreeController.cpp | 3 +++ src/controller/DreeControllerI.h | 1 + src/controller/HelpControllerI.h | 1 + src/controller/SearchController.cpp | 3 +++ src/controller/SearchControllerI.h | 1 + src/controller/navigate/DreeNavigate.cpp | 3 +++ src/controller/navigate/IDreeNavigate.h | 1 + src/data_structures/DreeNode.h | 6 +++++ src/main.cpp | 30 ++++++++++++++++++++++++ src/model/SearchDirectory.cpp | 21 ++++++++++++----- src/model/SearchDirectory.h | 4 ++-- src/model/SearchDirectoryI.h | 2 +- src/view/AboutDreeI.h | 1 + src/view/SearchResults.cpp | 6 ++--- src/view/SearchResults.h | 2 +- src/view/SearchResultsI.h | 2 +- 16 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/controller/DreeController.cpp b/src/controller/DreeController.cpp index d1faf1c..e02a1b1 100644 --- a/src/controller/DreeController.cpp +++ b/src/controller/DreeController.cpp @@ -3,6 +3,9 @@ void DreeController::print_dree(Args* args) { DreeNode* rootNode = DreeLoaderInterface->load_dree(args); DreePrinterInterface->depth_first_search(rootNode, args->MaxDepth); + + delete rootNode; + rootNode = nullptr; } DreeController::DreeController(DreeLoaderI* DreeLoader, PrintDreeI* DreePrinter) { diff --git a/src/controller/DreeControllerI.h b/src/controller/DreeControllerI.h index 519a85e..9754f84 100644 --- a/src/controller/DreeControllerI.h +++ b/src/controller/DreeControllerI.h @@ -11,6 +11,7 @@ using namespace std; class DreeControllerI { public: virtual void print_dree(Args* args) = 0; + virtual ~DreeControllerI() = default; }; #endif \ No newline at end of file diff --git a/src/controller/HelpControllerI.h b/src/controller/HelpControllerI.h index 58db553..8fa21fb 100644 --- a/src/controller/HelpControllerI.h +++ b/src/controller/HelpControllerI.h @@ -4,6 +4,7 @@ class HelpControllerI { public: virtual void help() = 0; + virtual ~HelpControllerI() = default; }; #endif \ No newline at end of file diff --git a/src/controller/SearchController.cpp b/src/controller/SearchController.cpp index e489a7a..0c7b7aa 100644 --- a/src/controller/SearchController.cpp +++ b/src/controller/SearchController.cpp @@ -13,4 +13,7 @@ void SearchController::search(string& query, Args* args) { DreeNode* root = new DreeNode(args->currentPath); auto searchResult = searchModel->search(root, query, dreeHelpers); searchResulPrinter->print_search_results(searchResult); + + delete root; + root = nullptr; } \ No newline at end of file diff --git a/src/controller/SearchControllerI.h b/src/controller/SearchControllerI.h index 93f19a1..3731dfa 100644 --- a/src/controller/SearchControllerI.h +++ b/src/controller/SearchControllerI.h @@ -9,6 +9,7 @@ using namespace std; class SearchControllerI { public: virtual void search(string& query, Args* args) = 0; + virtual ~SearchControllerI() = default; }; #endif \ No newline at end of file diff --git a/src/controller/navigate/DreeNavigate.cpp b/src/controller/navigate/DreeNavigate.cpp index d31d5f4..4193b28 100644 --- a/src/controller/navigate/DreeNavigate.cpp +++ b/src/controller/navigate/DreeNavigate.cpp @@ -3,6 +3,9 @@ void DreeNavigate::display_dree(Args* args) { DreeNode* rootNode = DreeLoaderInterface->load_dree(args); DreePrinterInterface->navigate_dree(rootNode, args->MaxDepth); + + delete rootNode; + rootNode = nullptr; } DreeNavigate::DreeNavigate(DreeLoaderI* DreeLoader, IDreeNavigateView* DreePrinter) { diff --git a/src/controller/navigate/IDreeNavigate.h b/src/controller/navigate/IDreeNavigate.h index 37b139e..5ade3c2 100644 --- a/src/controller/navigate/IDreeNavigate.h +++ b/src/controller/navigate/IDreeNavigate.h @@ -11,6 +11,7 @@ using namespace std; class IDreeNavigate { public: virtual void display_dree(Args* args) = 0; + virtual ~IDreeNavigate() = default; }; #endif \ No newline at end of file diff --git a/src/data_structures/DreeNode.h b/src/data_structures/DreeNode.h index a494b15..210b79a 100644 --- a/src/data_structures/DreeNode.h +++ b/src/data_structures/DreeNode.h @@ -17,6 +17,12 @@ class DreeNode { DreeNode *right; DreeNode(std::string &str); + + ~DreeNode() { + for (auto i : children) { + delete i; + } + } }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 04b13f7..6928606 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,12 +52,17 @@ int main(int argc, char* argv[]) { // #endif if (argc == 3 && strcmp(argv[2], "--help") == 0) { + cout<<"done 1"; string flag = argv[2]; AboutDree aboutView; HelpControllerI* controller = new HelpController(&aboutView); controller->help(); + + delete controller; + controller = nullptr; } else if (argc == 3 || (argc == 4 && strcmp(argv[3], "-n") != 0)) { + cout<<"done 3"; Args* arg = new Args(stoll(argv[2]), argv[1]); if (arg->MaxDepth > 60) { cout << "Depth overflow!!\nAre you serious?" << endl; @@ -73,9 +78,19 @@ int main(int argc, char* argv[]) { DreeControllerI* controller = new DreeController(&dreeLoader, &dreePrinter); controller->print_dree(arg); + delete arg; + arg = nullptr; + + delete dreeIgnore; + dreeIgnore = nullptr; + + delete controller; + controller = nullptr; + return 0; } else if (argc >= 3 && argc <= 5 && strcmp(argv[3], "-f") != 0) { + cout<<"done 3?\n"; if ((argc >= 4) && strcmp(argv[3], "-n") == 0) { Args* arg = new Args(stoll(argv[2]), argv[1]); if (arg->MaxDepth > 60) { @@ -92,10 +107,20 @@ int main(int argc, char* argv[]) { IDreeNavigate* controller = new DreeNavigate(&dreeLoader, dreeNavigateView); controller->display_dree(arg); + delete arg; + arg = nullptr; + + delete dreeIgnore; + dreeIgnore = nullptr; + + delete controller; + controller = nullptr; + return 0; } } else if (argc == 5 && strcmp(argv[3], "-f") == 0) { + cout<<"done 2\n"; DreeHelpers dreeHelpers; SearchResults searchResulPrinter; Args* args = new Args(stoll(argv[2]), argv[1]); @@ -106,6 +131,11 @@ int main(int argc, char* argv[]) { string query = argv[4]; searchController->search(query, args); + delete args; + args = nullptr; + + delete searchController; + searchController = nullptr; } else { cout << "Command Not Found!\n Run: Dree --help to learn more\n"; diff --git a/src/model/SearchDirectory.cpp b/src/model/SearchDirectory.cpp index c935887..d14a312 100644 --- a/src/model/SearchDirectory.cpp +++ b/src/model/SearchDirectory.cpp @@ -1,13 +1,13 @@ #include "SearchDirectory.h" #include -vector> SearchDirectory::search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) { - vector> searchResult; +vector>> SearchDirectory::search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) { + vector>> searchResult; traverse_directories(root, args->MaxDepth, 0, query, searchResult, dreeHelpers); return searchResult; } void SearchDirectory::traverse_directories(DreeNode *node, long long depth, long long currentDepth, const string &query, - vector> &results, DreeHelpersI *dreehelpers) { + vector>> &results, DreeHelpersI *dreehelpers) { if (currentDepth > depth) { return; } @@ -15,22 +15,31 @@ void SearchDirectory::traverse_directories(DreeNode *node, long long depth, long int score2 = dreehelpers->levenshtein_distance_between_strings(node->path, query); if (score1 * 100 <= (50 * query.length()) || score2 * 100 <= (50 * query.length())) { - results.push_back({min(score1, score2), node}); + results.push_back({min(score1, score2), {node->name, node->path}}); } if (!dreehelpers->string_is_a_directory(node->path)) { return; } + DreeNode *child; + try { + for (const auto &entry : filesystem::directory_iterator(node->path)) { string childDirectory = entry.path().string(); - DreeNode *child = new DreeNode(childDirectory); + child = new DreeNode(childDirectory); node->children.push_back(child); traverse_directories(child, depth, currentDepth + 1, query, results, dreehelpers); + + delete child; + child = nullptr; + node->children.pop_back(); } } catch (const std::exception &e) { - } + delete child; + child = nullptr; + } } SearchDirectory::SearchDirectory(Args *args) { this->args = args; } \ No newline at end of file diff --git a/src/model/SearchDirectory.h b/src/model/SearchDirectory.h index 30fe4c4..feb7c29 100644 --- a/src/model/SearchDirectory.h +++ b/src/model/SearchDirectory.h @@ -16,10 +16,10 @@ class SearchDirectory : public SearchDirectoryI { Args *args; void traverse_directories(DreeNode *node, long long depth, long long currentDepth, const string &query, - vector> &results, DreeHelpersI *dreehelpers); + vector>> &results, DreeHelpersI *dreehelpers); public: - vector> search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) override; + vector>> search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) override; SearchDirectory(Args *args); }; diff --git a/src/model/SearchDirectoryI.h b/src/model/SearchDirectoryI.h index 876d624..9a32b6e 100644 --- a/src/model/SearchDirectoryI.h +++ b/src/model/SearchDirectoryI.h @@ -10,7 +10,7 @@ using namespace std; class SearchDirectoryI { public: - virtual vector> search(DreeNode* root, string& query, DreeHelpersI* dreeHelpers) = 0; + virtual vector>> search(DreeNode* root, string& query, DreeHelpersI* dreeHelpers) = 0; }; #endif \ No newline at end of file diff --git a/src/view/AboutDreeI.h b/src/view/AboutDreeI.h index 747f522..ef20543 100644 --- a/src/view/AboutDreeI.h +++ b/src/view/AboutDreeI.h @@ -4,6 +4,7 @@ class AboutDreeI { public: virtual void print_help_instructions() = 0; + virtual ~AboutDreeI() = default; }; #endif diff --git a/src/view/SearchResults.cpp b/src/view/SearchResults.cpp index e7ea983..848c9f7 100644 --- a/src/view/SearchResults.cpp +++ b/src/view/SearchResults.cpp @@ -2,7 +2,7 @@ #include #include -void SearchResults::print_search_results(vector> &searchResult) { +void SearchResults::print_search_results(vector>> &searchResult) { sort(searchResult.begin(), searchResult.end()); if (searchResult.empty()) { cout << "No results found\n"; @@ -14,7 +14,7 @@ void SearchResults::print_search_results(vector> &searchRe if (res.first > 0) break; cout << cnt + 1 << ".\t"; - cout << res.second->name << "\t\t" << res.second->path << "\n"; + cout << res.second.first << "\t\t" << res.second.second << "\n"; cnt++; } } else { @@ -22,7 +22,7 @@ void SearchResults::print_search_results(vector> &searchRe int cnt = 0; for (auto res : searchResult) { cout << cnt + 1 << ".\t"; - cout << res.second->name << "\t\t" << res.second->path << "\n"; + cout << res.second.first << "\t\t" << res.second.second << "\n"; cnt++; } } diff --git a/src/view/SearchResults.h b/src/view/SearchResults.h index d000617..5c7085d 100644 --- a/src/view/SearchResults.h +++ b/src/view/SearchResults.h @@ -9,7 +9,7 @@ using namespace std; class SearchResults : public SearchResultsI { public: - void print_search_results(vector> &searchResult) override; + void print_search_results(vector>> &searchResult) override; }; #endif \ No newline at end of file diff --git a/src/view/SearchResultsI.h b/src/view/SearchResultsI.h index dfdb6bc..3ad72e4 100644 --- a/src/view/SearchResultsI.h +++ b/src/view/SearchResultsI.h @@ -5,7 +5,7 @@ class SearchResultsI { public: - virtual void print_search_results(std::vector> &searchResult) = 0; + virtual void print_search_results(std::vector>> &searchResult) = 0; }; #endif \ No newline at end of file From 5463fbcd19e55bfbbbe8ae033d4fe3028ee8fa9d Mon Sep 17 00:00:00 2001 From: ssupraja777 Date: Sat, 5 Oct 2024 07:54:09 +0530 Subject: [PATCH 2/2] code refactor --- src/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6928606..b2223e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,6 @@ int main(int argc, char* argv[]) { // #endif if (argc == 3 && strcmp(argv[2], "--help") == 0) { - cout<<"done 1"; string flag = argv[2]; AboutDree aboutView; HelpControllerI* controller = new HelpController(&aboutView); @@ -62,7 +61,6 @@ int main(int argc, char* argv[]) { controller = nullptr; } else if (argc == 3 || (argc == 4 && strcmp(argv[3], "-n") != 0)) { - cout<<"done 3"; Args* arg = new Args(stoll(argv[2]), argv[1]); if (arg->MaxDepth > 60) { cout << "Depth overflow!!\nAre you serious?" << endl; @@ -90,7 +88,6 @@ int main(int argc, char* argv[]) { return 0; } else if (argc >= 3 && argc <= 5 && strcmp(argv[3], "-f") != 0) { - cout<<"done 3?\n"; if ((argc >= 4) && strcmp(argv[3], "-n") == 0) { Args* arg = new Args(stoll(argv[2]), argv[1]); if (arg->MaxDepth > 60) { @@ -120,7 +117,6 @@ int main(int argc, char* argv[]) { } } else if (argc == 5 && strcmp(argv[3], "-f") == 0) { - cout<<"done 2\n"; DreeHelpers dreeHelpers; SearchResults searchResulPrinter; Args* args = new Args(stoll(argv[2]), argv[1]);