From f197892d4e94a51435aaa23ccca1817d293e0952 Mon Sep 17 00:00:00 2001 From: Roker Date: Fri, 26 Oct 2018 16:54:50 +0200 Subject: [PATCH] Sorry, but I do need that Deleter feature in the locked_queue to avoid memory leaks. But this way now it should not harm all non-users, I hope so. :-o --- locked_queue.hh | 9 ++++++++- test_library.cc | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/locked_queue.hh b/locked_queue.hh index a84cfdb..970b1f1 100644 --- a/locked_queue.hh +++ b/locked_queue.hh @@ -9,7 +9,7 @@ namespace utility { - template + template class locked_queue { typedef std::recursive_mutex Mutex; @@ -28,6 +28,13 @@ namespace utility void clear() { Lock L(_mtx); + if(Deleter != nullptr) + { + for(auto q : _q) + { + Deleter(q); + } + } _q.clear(); } diff --git a/test_library.cc b/test_library.cc index aa20382..3b5a56f 100644 --- a/test_library.cc +++ b/test_library.cc @@ -50,9 +50,10 @@ void consumer_thread() int sum = 0; while(keep_running) { - auto q = pc.get_changed(); // might block, that is okay. - switch(q.state()) + for(auto& q : pc) { + switch(q.state()) + { case PC_State::Created: { const int value = atoi( q.pdata->data ); @@ -84,6 +85,7 @@ void consumer_thread() break; } default: throw "Illegal state"; + } } }