diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 4c3cfeaa586..00b669aa1cf 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -570,11 +570,18 @@ Resource::Resource() : remapped_list(this) {} Resource::~Resource() { - if (!path_cache.is_empty()) { - ResourceCache::lock.lock(); - ResourceCache::resources.erase(path_cache); - ResourceCache::lock.unlock(); + if (unlikely(path_cache.is_empty())) { + return; } + + ResourceCache::lock.lock(); + // Only unregister from the cache if this is the actual resource listed there. + // (Other resources can have the same value in `path_cache` if loaded with `CACHE_IGNORE`.) + HashMap::Iterator E = ResourceCache::resources.find(path_cache); + if (likely(E && E->value == this)) { + ResourceCache::resources.remove(E); + } + ResourceCache::lock.unlock(); } HashMap ResourceCache::resources;