Skip to content

Commit

Permalink
Fix bug in ResourceAllocator with erasing iterators inside loops (#8223)
Browse files Browse the repository at this point in the history
  • Loading branch information
emezeske authored Oct 24, 2024
1 parent 0168823 commit 8d02cd0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions filament/src/ResourceAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void ResourceAllocator::gc(bool skippedFrame) noexcept {
if ((ageDiff >= MAX_AGE_SKIPPED_FRAME && skippedFrame) ||
(ageDiff >= mCacheMaxAge && evictedCount < MAX_EVICTION_COUNT)) {
evictedCount++;
purge(it);
it = purge(it);
} else {
// build the set of ages present in the cache after eviction
ages.set(std::min(size_t(31), ageDiff));
Expand All @@ -280,7 +280,7 @@ void ResourceAllocator::gc(bool skippedFrame) noexcept {
for (auto it = textureCache.begin(); it != textureCache.end();) {
const size_t ageDiff = age - it->second.age;
if (ageDiff >= maxAge) {
purge(it);
it = purge(it);
} else {
++it;
}
Expand All @@ -306,12 +306,13 @@ void ResourceAllocator::dump(bool brief) const noexcept {
}
}

void ResourceAllocator::purge(
ResourceAllocator::CacheContainer::iterator
ResourceAllocator::purge(
ResourceAllocator::CacheContainer::iterator const& pos) {
//slog.d << "purging " << pos->second.handle.getId() << ", age=" << pos->second.age << io::endl;
mBackend.destroyTexture(pos->second.handle);
mCacheSize -= pos->second.size;
mTextureCache.erase(pos);
return mTextureCache.erase(pos);
}

// ------------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion filament/src/ResourceAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ class ResourceAllocator final : public ResourceAllocatorInterface {

using CacheContainer = AssociativeContainer<TextureKey, TextureCachePayload>;

void purge(ResourceAllocator::CacheContainer::iterator const& pos);
ResourceAllocator::CacheContainer::iterator
purge(ResourceAllocator::CacheContainer::iterator const& pos);

backend::DriverApi& mBackend;
std::shared_ptr<ResourceAllocatorDisposer> mDisposer;
Expand Down

0 comments on commit 8d02cd0

Please sign in to comment.