From 031adad3ef185c5d89d118efc49f2c9b21787abd Mon Sep 17 00:00:00 2001 From: richterr Date: Tue, 23 Jun 2020 18:30:34 +0200 Subject: [PATCH] fixed memory access violation --- include/common.h | 22 +++++++++++++++++-- include/file_io.h | 1 + src/config.cpp | 13 +++++------ src/config_section.cpp | 11 ++-------- .../console/states/generator_selector.cpp | 1 + .../states/network_device_selector.cpp | 1 + src/frontend/console/states/output_setup.cpp | 1 + 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/include/common.h b/include/common.h index 91d0d0f..20f8d2a 100644 --- a/include/common.h +++ b/include/common.h @@ -78,7 +78,7 @@ namespace hyenae /* Methods */ - template< class T > + template< class T> void safe_delete(T*& ptr) { if (ptr != NULL) @@ -88,7 +88,25 @@ namespace hyenae } } /* safe_delete */ - + + template< class T> + void safe_delete(vector_t pointers) + { + T* current = NULL; + + while (pointers.size() > 0) + { + current = pointers.front(); + pointers.erase(pointers.begin()); + + if (current != NULL) + { + delete current; + } + } + + } /* free_vector_pointers */ + int64_t to_ms(duration_t duration); string_t to_ms_string(duration_t duration); diff --git a/include/file_io.h b/include/file_io.h index 9ffa5ea..64c6679 100644 --- a/include/file_io.h +++ b/include/file_io.h @@ -38,6 +38,7 @@ namespace hyenae public: using provider = func_t; + virtual ~file_io() {} virtual bool is_open() const = 0; virtual void open(const string_t& filename, bool overwrite) = 0; virtual void close() noexcept = 0; diff --git a/src/config.cpp b/src/config.cpp index d3c67fa..aa8dda2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -171,15 +171,14 @@ namespace hyenae } catch (const exception_t& exception) { - // TODO: Implement common method to free vector list of pointers, - // use method in all other classes too. - /* - for (auto section : sections) + if (sections.size() > 0) { - safe_delete(section); - } - */ + // It is only required to delete the root section since all + // sub-sections will be delete by it's destructor. + safe_delete(sections.front()); + } + throw runtime_error_t(exception.what()); } diff --git a/src/config_section.cpp b/src/config_section.cpp index de581c9..48cc38d 100644 --- a/src/config_section.cpp +++ b/src/config_section.cpp @@ -41,15 +41,8 @@ namespace hyenae config::section::~section() { - for (auto section : _sub_sections) - { - delete section; - } - - for (auto value : _values) - { - delete value; - } + safe_delete(_sub_sections); + safe_delete(_values); } /* ~section */ diff --git a/src/frontend/console/states/generator_selector.cpp b/src/frontend/console/states/generator_selector.cpp index bf67433..04500cb 100644 --- a/src/frontend/console/states/generator_selector.cpp +++ b/src/frontend/console/states/generator_selector.cpp @@ -165,6 +165,7 @@ namespace hyenae::frontend::console::states generator_selector::~generator_selector() { + // TODO: Replace with specific safe_delete method implementation for (auto item : _menu_items) { delete item.first; diff --git a/src/frontend/console/states/network_device_selector.cpp b/src/frontend/console/states/network_device_selector.cpp index 0d7bc88..22a6ca1 100644 --- a/src/frontend/console/states/network_device_selector.cpp +++ b/src/frontend/console/states/network_device_selector.cpp @@ -60,6 +60,7 @@ namespace hyenae::frontend::console::states network_device_selector::~network_device_selector() { + // TODO: Replace with specific safe_delete method implementation for (auto item : _menu_items) { delete item.first; diff --git a/src/frontend/console/states/output_setup.cpp b/src/frontend/console/states/output_setup.cpp index 92f81c5..6f6d99f 100644 --- a/src/frontend/console/states/output_setup.cpp +++ b/src/frontend/console/states/output_setup.cpp @@ -78,6 +78,7 @@ namespace hyenae::frontend::console::states output_setup::~output_setup() { + // TODO: Replace with specific safe_delete method implementation for (auto item : _menu_items) { delete item.first;