From fcde92ad89af44a9724a5ebf9fbfdf59b6e41d68 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 11 May 2018 09:00:22 +0100 Subject: [PATCH] Fixed crash the occurred when passing in a osgDB::Options to the ObjectCache that doesn't have any references to it. --- include/osgDB/ObjectCache | 7 +++++-- src/osgDB/ObjectCache.cpp | 26 ++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/osgDB/ObjectCache b/include/osgDB/ObjectCache index fc068707e..4a656ba8c 100644 --- a/include/osgDB/ObjectCache +++ b/include/osgDB/ObjectCache @@ -70,14 +70,17 @@ class OSGDB_EXPORT ObjectCache : public osg::Referenced typedef std::pair > FileNameOptionsPair; - class ClassComp { - public: + struct ClassComp + { bool operator() (const ObjectCache::FileNameOptionsPair& lhs, const ObjectCache::FileNameOptionsPair& rhs) const; }; + typedef std::pair, double > ObjectTimeStampPair; typedef std::map ObjectCacheMap; + ObjectCacheMap::iterator find(const std::string& fileName, const osgDB::Options* options); + ObjectCacheMap _objectCache; OpenThreads::Mutex _objectCacheMutex; diff --git a/src/osgDB/ObjectCache.cpp b/src/osgDB/ObjectCache.cpp index 4736a039f..217d3e5de 100644 --- a/src/osgDB/ObjectCache.cpp +++ b/src/osgDB/ObjectCache.cpp @@ -75,10 +75,29 @@ void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object OSG_DEBUG<<"Adding "<getOptionString() : "")<<"' to ObjectCache "<first.first==fileName) + { + if (itr->first.second.valid()) + { + if (options && *(itr->first.second)==*options) return itr; + } + else if (!options) return itr; + } + } + return _objectCache.end(); +} + + osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName, const Options *options) { OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options)); + ObjectCacheMap::iterator itr = find(fileName, options); if (itr!=_objectCache.end()) { osg::ref_ptr o = itr->first.second; @@ -98,8 +117,7 @@ osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName, const osg::ref_ptr ObjectCache::getRefFromObjectCache(const std::string& fileName, const Options *options) { OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCacheMap::iterator itr; - itr = _objectCache.find(FileNameOptionsPair(fileName, options)); + ObjectCacheMap::iterator itr = find(fileName, options); if (itr!=_objectCache.end()) { osg::ref_ptr o = itr->first.second; @@ -160,7 +178,7 @@ void ObjectCache::removeExpiredObjectsInCache(double expiryTime) void ObjectCache::removeFromObjectCache(const std::string& fileName, const Options *options) { OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options)); + ObjectCacheMap::iterator itr = find(fileName, options); if (itr!=_objectCache.end()) _objectCache.erase(itr); }