Fixed crash the occurred when passing in a osgDB::Options to the ObjectCache that doesn't have any references to it.

This commit is contained in:
Robert Osfield 2018-05-11 09:00:22 +01:00
parent 0fc7aa8cc0
commit fcde92ad89
2 changed files with 27 additions and 6 deletions

View File

@ -70,14 +70,17 @@ class OSGDB_EXPORT ObjectCache : public osg::Referenced
typedef std::pair<std::string, osg::ref_ptr<const osgDB::Options> > FileNameOptionsPair;
class ClassComp {
public:
struct ClassComp
{
bool operator() (const ObjectCache::FileNameOptionsPair& lhs, const ObjectCache::FileNameOptionsPair& rhs) const;
};
typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
typedef std::map<FileNameOptionsPair, ObjectTimeStampPair, ClassComp> ObjectCacheMap;
ObjectCacheMap::iterator find(const std::string& fileName, const osgDB::Options* options);
ObjectCacheMap _objectCache;
OpenThreads::Mutex _objectCacheMutex;

View File

@ -75,10 +75,29 @@ void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object
OSG_DEBUG<<"Adding "<<filename<<" with options '"<<(options ? options->getOptionString() : "")<<"' to ObjectCache "<<this<<std::endl;
}
ObjectCache::ObjectCacheMap::iterator ObjectCache::find(const std::string& fileName, const osgDB::Options* options)
{
for(ObjectCacheMap::iterator itr = _objectCache.begin();
itr != _objectCache.end();
++itr)
{
if (itr->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<OpenThreads::Mutex> lock(_objectCacheMutex);
ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options));
ObjectCacheMap::iterator itr = find(fileName, options);
if (itr!=_objectCache.end())
{
osg::ref_ptr<const osgDB::Options> o = itr->first.second;
@ -98,8 +117,7 @@ osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName, const
osg::ref_ptr<osg::Object> ObjectCache::getRefFromObjectCache(const std::string& fileName, const Options *options)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
ObjectCacheMap::iterator itr;
itr = _objectCache.find(FileNameOptionsPair(fileName, options));
ObjectCacheMap::iterator itr = find(fileName, options);
if (itr!=_objectCache.end())
{
osg::ref_ptr<const osgDB::Options> 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<OpenThreads::Mutex> lock(_objectCacheMutex);
ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options));
ObjectCacheMap::iterator itr = find(fileName, options);
if (itr!=_objectCache.end()) _objectCache.erase(itr);
}