From 33ed29bf34228da630b3393627463db24323fd41 Mon Sep 17 00:00:00 2001 From: ccr <735819385@qq.com> Date: Wed, 8 Jan 2025 17:33:09 +0800 Subject: [PATCH] fix: destroy task after complete, to release potential shared ptr holding --- cyber/base/bounded_queue.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cyber/base/bounded_queue.h b/cyber/base/bounded_queue.h index 4c2e4accd80..b35d34b7282 100644 --- a/cyber/base/bounded_queue.h +++ b/cyber/base/bounded_queue.h @@ -71,6 +71,18 @@ class BoundedQueue { T* pool_ = nullptr; std::unique_ptr wait_strategy_ = nullptr; volatile bool break_all_wait_ = false; + + template + typename std::enable_if::value, void>::type + assign(T* element, uint64_t new_head) { + *element = std::move(pool_[GetIndex(new_head)]); + } + + template + typename std::enable_if::value, void>::type + assign(T* element, uint64_t new_head) { + *element = pool_[GetIndex(new_head)]; + } }; template @@ -161,10 +173,10 @@ bool BoundedQueue::Dequeue(T* element) { if (new_head == commit_.load(std::memory_order_acquire)) { return false; } - *element = pool_[GetIndex(new_head)]; } while (!head_.compare_exchange_weak(old_head, new_head, std::memory_order_acq_rel, std::memory_order_relaxed)); + assign(element, new_head); return true; }