diff --git a/apps/openmw/mwrender/landmanager.cpp b/apps/openmw/mwrender/landmanager.cpp index c2775679bc..6ab9f12139 100644 --- a/apps/openmw/mwrender/landmanager.cpp +++ b/apps/openmw/mwrender/landmanager.cpp @@ -19,9 +19,8 @@ namespace MWRender osg::ref_ptr LandManager::getLand(ESM::ExteriorCellLocation cellIndex) { - const osg::ref_ptr obj = mCache->getRefFromObjectCache(cellIndex); - if (obj != nullptr) - return static_cast(obj.get()); + if (const std::optional> obj = mCache->getRefFromObjectCacheOrNone(cellIndex)) + return static_cast(obj->get()); const MWBase::World& world = *MWBase::Environment::get().getWorld(); osg::ref_ptr landObj = nullptr; @@ -29,16 +28,14 @@ namespace MWRender if (ESM::isEsm4Ext(cellIndex.mWorldspace)) { const ESM4::Land* land = world.getStore().get().search(cellIndex); - if (land == nullptr) - return nullptr; - landObj = new ESMTerrain::LandObject(*land, mLoadFlags); + if (land != nullptr) + landObj = new ESMTerrain::LandObject(*land, mLoadFlags); } else { const ESM::Land* land = world.getStore().get().search(cellIndex.mX, cellIndex.mY); - if (land == nullptr) - return nullptr; - landObj = new ESMTerrain::LandObject(*land, mLoadFlags); + if (land != nullptr) + landObj = new ESMTerrain::LandObject(*land, mLoadFlags); } mCache->addEntryToObjectCache(cellIndex, landObj.get()); diff --git a/components/resource/objectcache.hpp b/components/resource/objectcache.hpp index 6c155b9ec6..3847129ed3 100644 --- a/components/resource/objectcache.hpp +++ b/components/resource/objectcache.hpp @@ -26,6 +26,7 @@ #include #include +#include #include namespace osg @@ -82,7 +83,8 @@ namespace Resource { if (oitr->second.second <= expiryTime) { - objectsToRemove.push_back(oitr->second.first); + if (oitr->second.first != nullptr) + objectsToRemove.push_back(oitr->second.first); _objectCache.erase(oitr++); } else @@ -127,6 +129,15 @@ namespace Resource return nullptr; } + std::optional> getRefFromObjectCacheOrNone(const KeyType& key) + { + const std::lock_guard lock(_objectCacheMutex); + const auto it = _objectCache.find(key); + if (it == _objectCache.end()) + return std::nullopt; + return it->second.first; + } + /** Check if an object is in the cache, and if it is, update its usage time stamp. */ bool checkInObjectCache(const KeyType& key, double timeStamp) {