Skip to content

Commit 33ed29b

Browse files
superMan-1012ccr
authored andcommitted
fix: destroy task after complete, to release potential shared ptr holding
1 parent 38ae485 commit 33ed29b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

cyber/base/bounded_queue.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ class BoundedQueue {
7171
T* pool_ = nullptr;
7272
std::unique_ptr<WaitStrategy> wait_strategy_ = nullptr;
7373
volatile bool break_all_wait_ = false;
74+
75+
template<typename U = T>
76+
typename std::enable_if<std::is_move_assignable<U>::value, void>::type
77+
assign(T* element, uint64_t new_head) {
78+
*element = std::move(pool_[GetIndex(new_head)]);
79+
}
80+
81+
template<typename U = T>
82+
typename std::enable_if<!std::is_move_assignable<U>::value, void>::type
83+
assign(T* element, uint64_t new_head) {
84+
*element = pool_[GetIndex(new_head)];
85+
}
7486
};
7587

7688
template <typename T>
@@ -161,10 +173,10 @@ bool BoundedQueue<T>::Dequeue(T* element) {
161173
if (new_head == commit_.load(std::memory_order_acquire)) {
162174
return false;
163175
}
164-
*element = pool_[GetIndex(new_head)];
165176
} while (!head_.compare_exchange_weak(old_head, new_head,
166177
std::memory_order_acq_rel,
167178
std::memory_order_relaxed));
179+
assign(element, new_head);
168180
return true;
169181
}
170182

0 commit comments

Comments
 (0)