Skip to content

Commit

Permalink
Modernize function ObjectCache::DeleteUnusedObjects (fix issue with s…
Browse files Browse the repository at this point in the history
…anitizers)

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<type-parameter-0-0, type-parameter-0-1,
    std::bidirectional_iterator_tag>
    &__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<tesseract::ObjectCache<tesseract::Dawg>::ReferenceCount
    *,
    std::__cxx1998::vector<tesseract::ObjectCache<tesseract::Dawg>::ReferenceCount,
    std::allocator<tesseract::ObjectCache<tesseract::Dawg>::ReferenceCount>>>,
[...]

That error message was followed by an endless sequence of newlines.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Dec 12, 2022
1 parent b37de16 commit 8c34b0d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/ccutil/object_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ class ObjectCache {

void DeleteUnusedObjects() {
std::lock_guard<std::mutex> 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:
Expand Down

0 comments on commit 8c34b0d

Please sign in to comment.