From f7ac50cd5db38684b1cf9fd6a305b7d514ef1d97 Mon Sep 17 00:00:00 2001 From: jg0 Date: Wed, 13 Oct 2021 14:27:53 +0800 Subject: [PATCH] fix memory leak of dynamic creation and release of threads --- deps/oblib/src/lib/coro/co_routine.cpp | 9 +++++-- deps/oblib/src/lib/coro/co_routine.h | 3 +-- deps/oblib/src/lib/coro/co_var_center.cpp | 32 +++++++---------------- deps/oblib/src/lib/coro/co_var_center.h | 6 ++--- deps/oblib/src/lib/coro/thread.cpp | 3 +++ 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/deps/oblib/src/lib/coro/co_routine.cpp b/deps/oblib/src/lib/coro/co_routine.cpp index ce8562788a..9949793d21 100644 --- a/deps/oblib/src/lib/coro/co_routine.cpp +++ b/deps/oblib/src/lib/coro/co_routine.cpp @@ -144,13 +144,13 @@ void CoRoutine::__start(transfer_t from) int CoRoutine::at_create() { - CVC.at_routine_create(); + CVC.at_routine_create(get_crls_buffer()); return OB_SUCCESS; } void CoRoutine::at_exit() { - CVC.at_routine_exit(); + CVC.at_routine_exit(get_crls_buffer()); return; } @@ -190,6 +190,11 @@ void CoMainRoutine::start() free_coidx(idx_); } +void CoMainRoutine::at_exit() +{ + CoRoutine::at_exit(); +} + void CoMainRoutine::destroy() { CoCtx::destroy(&cc_); diff --git a/deps/oblib/src/lib/coro/co_routine.h b/deps/oblib/src/lib/coro/co_routine.h index 8884e454a8..c77c4a9a00 100644 --- a/deps/oblib/src/lib/coro/co_routine.h +++ b/deps/oblib/src/lib/coro/co_routine.h @@ -235,8 +235,7 @@ public: void start(); // Destroy resources. void destroy(); - void at_exit() override - {} + void at_exit() override; void usleep(uint32_t usec) final; void sleep_until(int64_t abs_time) final; diff --git a/deps/oblib/src/lib/coro/co_var_center.cpp b/deps/oblib/src/lib/coro/co_var_center.cpp index e6a6664e33..6a7d356389 100644 --- a/deps/oblib/src/lib/coro/co_var_center.cpp +++ b/deps/oblib/src/lib/coro/co_var_center.cpp @@ -72,35 +72,21 @@ int CoVarCenter::register_hook(CoVarHook* hook) return static_cast(num); } -void CoVarCenter::at_routine_create() +void CoVarCenter::at_routine_create(char *buffer) { - char* buffer = nullptr; - if (OB_LIKELY(CoSched::get_active_routine() != nullptr)) { - buffer = CoSched::get_active_routine()->get_crls_buffer(); - assert(buffer != nullptr); - } else { - ob_abort(); - } - - int64_t& num = reinterpret_cast(*(&buffer[coro::CoConfig::MAX_CRLS_SIZE] - 8)); + int64_t &num = reinterpret_cast(*(&buffer[coro::CoConfig::MAX_CRLS_SIZE]-8)); num = 0; } -void CoVarCenter::at_routine_exit() +void CoVarCenter::at_routine_exit(char *buffer) { - if (CoSched::get_active_routine() != CoSched::get_instance()) { - char* buffer = nullptr; - if (OB_LIKELY(CoSched::get_active_routine() != nullptr)) { - buffer = CoSched::get_active_routine()->get_crls_buffer(); - } - const auto MAX_CRLS_SIZE = coro::CoConfig::MAX_CRLS_SIZE; - int64_t& num = reinterpret_cast(*(&buffer[MAX_CRLS_SIZE] - 8)); - CoVarHook** last = reinterpret_cast(&buffer[MAX_CRLS_SIZE] - 16); - for (int64_t i = num - 1; i >= 0; i--) { - last[-i]->deinit_(); - } - num = 0; + const auto MAX_CRLS_SIZE = coro::CoConfig::MAX_CRLS_SIZE; + int64_t &num = reinterpret_cast(*(&buffer[MAX_CRLS_SIZE]-8)); + CoVarHook **last = reinterpret_cast(&buffer[MAX_CRLS_SIZE]-16); + for (int64_t i = num-1; i >= 0; i--) { + last[-i]->deinit_(); } + num = 0; } } // namespace lib diff --git a/deps/oblib/src/lib/coro/co_var_center.h b/deps/oblib/src/lib/coro/co_var_center.h index 3adcbffef6..dd7e07da48 100644 --- a/deps/oblib/src/lib/coro/co_var_center.h +++ b/deps/oblib/src/lib/coro/co_var_center.h @@ -72,9 +72,9 @@ public: return pos; } - int register_hook(CoVarHook* hook); - void at_routine_create(); - void at_routine_exit(); + int register_hook(CoVarHook *hook); + void at_routine_create(char *buffer); + void at_routine_exit(char *buffer); static CoVarCenter& instance() { diff --git a/deps/oblib/src/lib/coro/thread.cpp b/deps/oblib/src/lib/coro/thread.cpp index 487939ab11..06eda94e7e 100644 --- a/deps/oblib/src/lib/coro/thread.cpp +++ b/deps/oblib/src/lib/coro/thread.cpp @@ -229,6 +229,9 @@ void* Thread::__th_start(void* arg) } } } + if (mem_context != nullptr && *mem_context != nullptr) { + DESTROY_CONTEXT(*mem_context); + } } } -- GitLab