From 8c34b0de6270c115074402918c954b38e97354ba Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 12 Dec 2022 14:49:35 +0100 Subject: [PATCH] Modernize function ObjectCache::DeleteUnusedObjects (fix issue with sanitizers) The old code did not work with compiler option `-fsanitize=address,undefined` and caused apiexample_test to run forever with this error message: Running main() from unittest/third_party/googletest/googletest/src/gtest_main.cc [==========] Running 4 tests from 2 test suites. [----------] Global test environment set-up. [----------] 1 test from EuroText [ RUN ] EuroText.FastLatinOCR /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/debug/safe_iterator.h:608: In function: _Safe_iterator &__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator::ReferenceCount *, std::__cxx1998::vector::ReferenceCount, std::allocator::ReferenceCount>>>, [...] That error message was followed by an endless sequence of newlines. Signed-off-by: Stefan Weil --- src/ccutil/object_cache.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ccutil/object_cache.h b/src/ccutil/object_cache.h index 8d442e4c..ad2c7b38 100644 --- a/src/ccutil/object_cache.h +++ b/src/ccutil/object_cache.h @@ -96,12 +96,16 @@ public: void DeleteUnusedObjects() { std::lock_guard guard(mu_); - for (auto it = cache_.rbegin(); it != cache_.rend(); ++it) { - if (it->count <= 0) { - delete it->object; - cache_.erase(std::next(it).base()); - } - } + cache_.erase(std::remove_if(cache_.begin(), cache_.end(), + [](const ReferenceCount &it) { + if (it.count <= 0) { + delete it.object; + return true; + } else { + return false; + } + }), + cache_.end()); } private: