Skip to content

Commit

Permalink
Merge pull request #31 from AntelopeIO/dtor_order_40
Browse files Browse the repository at this point in the history
fix appbase destruction order: destroy executor after destroying plugins
  • Loading branch information
spoonincode authored Mar 11, 2024
2 parents b75b31e + 7fe91d3 commit d7a7580
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions application_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class application_impl {
std::optional<boost::asio::io_context> _signal_catching_io_ctx;
};

application_base::application_base()
:my(new application_impl()){
application_base::application_base(std::shared_ptr<void>&& e) :
executor_ptr(std::move(e)), my(new application_impl()){
register_config_type<std::string>();
register_config_type<bool>();
register_config_type<unsigned short>();
Expand Down
20 changes: 11 additions & 9 deletions include/appbase/application_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ class application_base {
}
///@}

application_base(); ///< protected because application is a singleton that should be accessed via instance()
application_base(std::shared_ptr<void>&& e); ///< protected because application is a singleton that should be accessed via instance()

/// !!! must be dtor'ed after plugins
std::shared_ptr<void> executor_ptr;

private:
// members are ordered taking into account that the last one is destructed first
Expand Down Expand Up @@ -350,15 +353,15 @@ class application_t : public application_base {
*/
template <typename Func>
auto post(int priority, Func&& func) {
return executor_.post(priority, std::forward<Func>(func));
return executor().post(priority, std::forward<Func>(func));
}

/**
* Wait until quit(), SIGINT or SIGTERM and then shutdown.
* Should only be executed from one thread.
*/
void exec() {
application_base::exec(executor_);
application_base::exec(executor());
}

/**
Expand All @@ -367,25 +370,24 @@ class application_t : public application_base {
* @return
*/
boost::asio::io_service& get_io_service() {
return executor_.get_io_service();
return executor().get_io_service();
}

void startup() {
application_base::startup(get_io_service());
}

application_t() {
application_t() : application_base(std::make_shared<executor_t>()) {
set_stop_executor_cb([&]() { get_io_service().stop(); });
set_post_cb([&](int prio, std::function<void()> cb) { executor_.post(prio, std::move(cb)); });
set_post_cb([&](int prio, std::function<void()> cb) { executor().post(prio, std::move(cb)); });
}

executor_t& executor() {
return executor_;
executor_t& executor() const {
return *static_cast<executor_t*>(executor_ptr.get());
}

private:
inline static std::unique_ptr<application_t> app_instance;
executor_t executor_;
};


Expand Down

0 comments on commit d7a7580

Please sign in to comment.