From 8361298e530ff29726cddbdbcc2bd379d0b90635 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:30:35 -0500 Subject: [PATCH 1/3] destroy executor after destroying plugins --- application_base.cpp | 4 ++-- include/appbase/application_base.hpp | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/application_base.cpp b/application_base.cpp index 14ccad3..89a175a 100644 --- a/application_base.cpp +++ b/application_base.cpp @@ -88,8 +88,8 @@ class application_impl { std::optional _signal_catching_io_ctx; }; -application_base::application_base() -:my(new application_impl()){ +application_base::application_base(std::shared_ptr&& e) : + executor_ptr(std::move(e)), my(new application_impl()){ register_config_type(); register_config_type(); register_config_type(); diff --git a/include/appbase/application_base.hpp b/include/appbase/application_base.hpp index 1efbb40..3b8da26 100644 --- a/include/appbase/application_base.hpp +++ b/include/appbase/application_base.hpp @@ -289,7 +289,10 @@ class application_base { } ///@} - application_base(); ///< protected because application is a singleton that should be accessed via instance() + application_base(std::shared_ptr&& e); ///< protected because application is a singleton that should be accessed via instance() + + /// !!! must be dtor'ed after plugins + std::shared_ptr executor_ptr; private: // members are ordered taking into account that the last one is destructed first @@ -368,7 +371,7 @@ class application_t : public application_base { application_base::startup(get_io_service()); } - application_t() { + application_t() : application_base(std::make_shared()), executor_(*static_cast(executor_ptr.get())) { set_stop_executor_cb([&]() { get_io_service().stop(); }); set_post_cb([&](int prio, std::function cb) { executor_.post(prio, std::move(cb)); }); } @@ -379,7 +382,7 @@ class application_t : public application_base { private: inline static std::unique_ptr app_instance; - executor_t executor_; + executor_t& executor_; }; From 7266b2d1cfb5c0119e035d24e0829e130f420713 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:12:23 -0500 Subject: [PATCH 2/3] don't store a executor_t& rather cast on each use --- include/appbase/application_base.hpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/appbase/application_base.hpp b/include/appbase/application_base.hpp index 3b8da26..e074755 100644 --- a/include/appbase/application_base.hpp +++ b/include/appbase/application_base.hpp @@ -352,7 +352,7 @@ class application_t : public application_base { */ template auto post(int priority, Func&& func) { - return executor_.post(priority, std::forward(func)); + return get_executor().post(priority, std::forward(func)); } /** @@ -360,29 +360,32 @@ class application_t : public application_base { * Should only be executed from one thread. */ void exec() { - application_base::exec(executor_); + application_base::exec(get_executor()); } boost::asio::io_service& get_io_service() { - return executor_.get_io_service(); + return get_executor().get_io_service(); } void startup() { application_base::startup(get_io_service()); } - application_t() : application_base(std::make_shared()), executor_(*static_cast(executor_ptr.get())) { + application_t() : application_base(std::make_shared()) { set_stop_executor_cb([&]() { get_io_service().stop(); }); - set_post_cb([&](int prio, std::function cb) { executor_.post(prio, std::move(cb)); }); + set_post_cb([&](int prio, std::function cb) { get_executor().post(prio, std::move(cb)); }); } executor_t& executor() { - return executor_; + return get_executor(); } private: inline static std::unique_ptr app_instance; - executor_t& executor_; + + executor_t& get_executor() const { + return *static_cast(executor_ptr.get()); + } }; From 7fe91d33fb590e4bfb5f04d4ec67bfc59f347a73 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 11 Mar 2024 13:27:27 -0400 Subject: [PATCH 3/3] just use executor() for what get_executor() was doing --- include/appbase/application_base.hpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/include/appbase/application_base.hpp b/include/appbase/application_base.hpp index e074755..825cdec 100644 --- a/include/appbase/application_base.hpp +++ b/include/appbase/application_base.hpp @@ -352,7 +352,7 @@ class application_t : public application_base { */ template auto post(int priority, Func&& func) { - return get_executor().post(priority, std::forward(func)); + return executor().post(priority, std::forward(func)); } /** @@ -360,11 +360,11 @@ class application_t : public application_base { * Should only be executed from one thread. */ void exec() { - application_base::exec(get_executor()); + application_base::exec(executor()); } boost::asio::io_service& get_io_service() { - return get_executor().get_io_service(); + return executor().get_io_service(); } void startup() { @@ -373,19 +373,15 @@ class application_t : public application_base { application_t() : application_base(std::make_shared()) { set_stop_executor_cb([&]() { get_io_service().stop(); }); - set_post_cb([&](int prio, std::function cb) { get_executor().post(prio, std::move(cb)); }); + set_post_cb([&](int prio, std::function cb) { executor().post(prio, std::move(cb)); }); } - executor_t& executor() { - return get_executor(); + executor_t& executor() const { + return *static_cast(executor_ptr.get()); } private: inline static std::unique_ptr app_instance; - - executor_t& get_executor() const { - return *static_cast(executor_ptr.get()); - } };