diff options
author | saturneric <[email protected]> | 2023-12-16 05:12:25 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-16 05:12:25 +0000 |
commit | c41074792f8c3b966b6d637c9e9b0ee10c5255e7 (patch) | |
tree | a20f44b3f275f69d0185a0dcf926082721db3220 | |
parent | fix: slove threading and memory issues (diff) | |
download | GpgFrontend-c41074792f8c3b966b6d637c9e9b0ee10c5255e7.tar.gz GpgFrontend-c41074792f8c3b966b6d637c9e9b0ee10c5255e7.zip |
fix: clean up envirnoment when app exits
-rw-r--r-- | src/core/function/basic/SingletonStorageCollection.cpp | 23 | ||||
-rw-r--r-- | src/core/function/basic/SingletonStorageCollection.h | 2 | ||||
-rw-r--r-- | src/core/module/GlobalRegisterTable.cpp | 9 | ||||
-rw-r--r-- | src/core/module/ModuleManager.cpp | 2 | ||||
-rw-r--r-- | src/core/module/ModuleManager.h | 4 | ||||
-rw-r--r-- | src/init.cpp | 2 | ||||
-rw-r--r-- | src/init.h | 13 | ||||
-rw-r--r-- | src/main.cpp | 10 |
8 files changed, 43 insertions, 22 deletions
diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp index e69c1279..9c0116cb 100644 --- a/src/core/function/basic/SingletonStorageCollection.cpp +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -31,14 +31,13 @@ #include <memory> #include <shared_mutex> +#include "core/function/SecureMemoryAllocator.h" #include "core/function/basic/SingletonStorage.h" #include "core/utils/MemoryUtils.h" namespace GpgFrontend { -std::unique_ptr<SingletonStorageCollection, - SecureObjectDeleter<SingletonStorageCollection>> - instance = nullptr; +SecureUniquePtr<SingletonStorageCollection> global_instance = nullptr; class SingletonStorageCollection::Impl { public: @@ -48,12 +47,13 @@ class SingletonStorageCollection::Impl { * @return SingletonStorageCollection* */ static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { - if (force_refresh || instance == nullptr) { - instance = SecureCreateUniqueObject<SingletonStorageCollection>(); - SPDLOG_TRACE("a new single storage collection created, address: {}", - static_cast<void*>(instance.get())); + if (force_refresh || global_instance == nullptr) { + global_instance = SecureCreateUniqueObject<SingletonStorageCollection>(); + SPDLOG_TRACE( + "a new global singleton storage collection created, address: {}", + static_cast<void*>(global_instance.get())); } - return instance.get(); + return global_instance.get(); } /** @@ -61,7 +61,7 @@ class SingletonStorageCollection::Impl { * * @return SingletonStorageCollection* */ - static void Destroy() { instance = nullptr; } + static void Destroy() { global_instance = nullptr; } /** * @brief Get the Singleton Storage object @@ -100,7 +100,7 @@ class SingletonStorageCollection::Impl { }; SingletonStorageCollection::SingletonStorageCollection() noexcept - : p_(std::make_unique<Impl>()) {} + : p_(SecureCreateUniqueObject<Impl>()) {} SingletonStorageCollection::~SingletonStorageCollection() = default; @@ -110,6 +110,9 @@ auto GpgFrontend::SingletonStorageCollection::GetInstance(bool force_refresh) } void SingletonStorageCollection::Destroy() { + SPDLOG_TRACE( + "global singleton storage collection is about to destroy, address: {}", + static_cast<void*>(global_instance.get())); return SingletonStorageCollection::Impl::Destroy(); } diff --git a/src/core/function/basic/SingletonStorageCollection.h b/src/core/function/basic/SingletonStorageCollection.h index 70b91cb9..38ced83b 100644 --- a/src/core/function/basic/SingletonStorageCollection.h +++ b/src/core/function/basic/SingletonStorageCollection.h @@ -73,7 +73,7 @@ class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection { private: class Impl; - std::unique_ptr<Impl> p_; + SecureUniquePtr<Impl> p_; }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/module/GlobalRegisterTable.cpp b/src/core/module/GlobalRegisterTable.cpp index 6f43aeb4..eda6c744 100644 --- a/src/core/module/GlobalRegisterTable.cpp +++ b/src/core/module/GlobalRegisterTable.cpp @@ -45,7 +45,7 @@ class GlobalRegisterTable::Impl { public: struct RTNode { std::optional<std::any> value = std::nullopt; - std::unordered_map<std::string, std::unique_ptr<RTNode>> children; + std::unordered_map<std::string, SecureUniquePtr<RTNode>> children; int version = 0; const std::type_info* type = nullptr; }; @@ -61,12 +61,13 @@ class GlobalRegisterTable::Impl { { std::unique_lock lock(lock_); auto& root_rt_node = - global_register_table_.emplace(n, std::make_unique<RTNode>()) + global_register_table_.emplace(n, SecureCreateUniqueObject<RTNode>()) .first->second; RTNode* current = root_rt_node.get(); while (std::getline(iss, segment, '.')) { - current = current->children.emplace(segment, std::make_unique<RTNode>()) + current = current->children + .emplace(segment, SecureCreateUniqueObject<RTNode>()) .first->second.get(); } @@ -137,7 +138,7 @@ class GlobalRegisterTable::Impl { } private: - using Table = std::map<Namespace, std::unique_ptr<RTNode>>; + using Table = std::map<Namespace, SecureUniquePtr<RTNode>>; std::shared_mutex lock_; GlobalRegisterTable* parent_; diff --git a/src/core/module/ModuleManager.cpp b/src/core/module/ModuleManager.cpp index 26edc43e..420bc611 100644 --- a/src/core/module/ModuleManager.cpp +++ b/src/core/module/ModuleManager.cpp @@ -141,7 +141,7 @@ auto ListRTChildKeys(const std::string& namespace_, const std::string& key) ModuleManager::ModuleManager(int channel) : SingletonFunctionObject<ModuleManager>(channel), - p_(std::make_unique<Impl>()) {} + p_(SecureCreateUniqueObject<Impl>()) {} ModuleManager::~ModuleManager() = default; diff --git a/src/core/module/ModuleManager.h b/src/core/module/ModuleManager.h index ace71c13..bf00c87c 100644 --- a/src/core/module/ModuleManager.h +++ b/src/core/module/ModuleManager.h @@ -31,8 +31,10 @@ #include <mutex> #include <vector> +#include "core/function/SecureMemoryAllocator.h" #include "core/function/basic/GpgFunctionObject.h" #include "core/module/Event.h" +#include "core/utils/MemoryUtils.h" namespace GpgFrontend::Thread { class TaskRunner; @@ -86,7 +88,7 @@ class GPGFRONTEND_CORE_EXPORT ModuleManager private: class Impl; - std::unique_ptr<Impl> p_; + SecureUniquePtr<Impl> p_; }; template <typename T, typename... Args> diff --git a/src/init.cpp b/src/init.cpp index 50fb5a87..f6d90d83 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -199,6 +199,8 @@ void ShutdownGlobalBasicalEnv(const GFCxtWPtr &p_ctx) { Thread::TaskRunnerGetter::GetInstance().StopAllTeakRunner(); + DestroyGpgFrontendCore(); + ShutdownLoggingSystem(ctx); } @@ -44,13 +44,13 @@ void HandleSignal(int sig); * * @param args */ -void InitLoggingSystem(const GFCxtSPtr&); +void InitLoggingSystem(const GFCxtSPtr &); /** * @brief initialize the logging system. * */ -void ShutdownLoggingSystem(const GFCxtSPtr&); +void ShutdownLoggingSystem(const GFCxtSPtr &); /** * @brief init global PATH env @@ -63,6 +63,13 @@ void InitGlobalPathEnv(); * * @param args */ -void InitGlobalBasicalEnv(const GFCxtWPtr&, bool); +void InitGlobalBasicalEnv(const GFCxtWPtr &, bool); + +/** + * @brief + * + * @param p_ctx + */ +void ShutdownGlobalBasicalEnv(const GFCxtWPtr &p_ctx); } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a8de6993..3bffe608 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,7 @@ auto main(int argc, char* argv[]) -> int { GpgFrontend::GFCxtSPtr ctx = GpgFrontend::SecureCreateSharedObject<GpgFrontend::GpgFrontendContext>( argc, argv); + auto rtn = 0; // initialize qt resources Q_INIT_RESOURCE(gpgfrontend); @@ -102,9 +103,14 @@ auto main(int argc, char* argv[]) -> int { ctx->load_default_gpg_context = false; InitGlobalBasicalEnv(ctx, false); - return RunTest(ctx); + rtn = RunTest(ctx); + ShutdownGlobalBasicalEnv(ctx); + return rtn; } InitGlobalBasicalEnv(ctx, true); - return StartApplication(ctx); + rtn = StartApplication(ctx); + ShutdownGlobalBasicalEnv(ctx); + + return rtn; } |