From b04edb70aa50e271bbf26473143d1208cd4b1856 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 18 Mar 2003 20:10:51 +0000 Subject: [PATCH] Added a clear of allocated Records and the cache's in flt::Registry to prevent memory leaks and unneccesary references to model models remaining beyond the scope of the loader. --- src/osgPlugins/flt/LocalVertexPoolRecord.cpp | 22 -------------------- src/osgPlugins/flt/LocalVertexPoolRecord.h | 3 --- src/osgPlugins/flt/ReaderWriterFLT.cpp | 6 +++++- src/osgPlugins/flt/Record.cpp | 8 +++++++ src/osgPlugins/flt/Record.h | 3 +++ src/osgPlugins/flt/Registry.cpp | 11 ++++++++-- src/osgPlugins/flt/Registry.h | 8 +++++++ src/osgPlugins/flt/flt2osg.cpp | 18 +++++++++++++--- src/osgPlugins/flt/flt2osg.h | 20 ++++++++++-------- 9 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/osgPlugins/flt/LocalVertexPoolRecord.cpp b/src/osgPlugins/flt/LocalVertexPoolRecord.cpp index 1a594eeeb..f30396c93 100644 --- a/src/osgPlugins/flt/LocalVertexPoolRecord.cpp +++ b/src/osgPlugins/flt/LocalVertexPoolRecord.cpp @@ -499,25 +499,3 @@ void LocalVertexPoolRecord::postReadInit() } -/////////////////////////////////////////////////////////////////////////////// -// -// Make this instance the current one. -// -/////////////////////////////////////////////////////////////////////////////// - -void LocalVertexPoolRecord::makeCurrent() -{ - _current = this; -} - - -/////////////////////////////////////////////////////////////////////////////// -// -// Return the current instance. -// -/////////////////////////////////////////////////////////////////////////////// - -LocalVertexPoolRecord *LocalVertexPoolRecord::getCurrent() -{ - return _current; -} diff --git a/src/osgPlugins/flt/LocalVertexPoolRecord.h b/src/osgPlugins/flt/LocalVertexPoolRecord.h index 44a6631d0..7c48aee52 100644 --- a/src/osgPlugins/flt/LocalVertexPoolRecord.h +++ b/src/osgPlugins/flt/LocalVertexPoolRecord.h @@ -69,9 +69,6 @@ public: virtual size_t sizeofData() const { return sizeof ( SLocalVertexPool ); } - void makeCurrent(); - static LocalVertexPoolRecord * getCurrent(); - protected: class Offset diff --git a/src/osgPlugins/flt/ReaderWriterFLT.cpp b/src/osgPlugins/flt/ReaderWriterFLT.cpp index ddcea735a..f37e7b42c 100644 --- a/src/osgPlugins/flt/ReaderWriterFLT.cpp +++ b/src/osgPlugins/flt/ReaderWriterFLT.cpp @@ -6,6 +6,7 @@ #include "ReaderWriterFLT.h" #include "FltFile.h" +#include "Registry.h" #include #include @@ -27,7 +28,7 @@ osgDB::ReaderWriter::ReadResult ReaderWriterFLT::readNode(const std::string& fil return ReadResult::FILE_NOT_HANDLED; osg::ref_ptr read = new FltFile; - + if (options) { read->setUseTextureAlphaForTransparancyBinning(options->getOptionString().find("noTextureAlphaForTransparancyBinning")==std::string::npos); @@ -35,6 +36,9 @@ osgDB::ReaderWriter::ReadResult ReaderWriterFLT::readNode(const std::string& fil } osg::Node* node = read->readNode(fileName); + + flt::Registry::instance()->clearObjectCache(); + if (node) return node; else return ReadResult::FILE_NOT_HANDLED; } diff --git a/src/osgPlugins/flt/Record.cpp b/src/osgPlugins/flt/Record.cpp index cdd8e74a5..4bb15c191 100644 --- a/src/osgPlugins/flt/Record.cpp +++ b/src/osgPlugins/flt/Record.cpp @@ -23,16 +23,24 @@ using namespace flt; // //////////////////////////////////////////////////////////////////// +int Record::s_numAllocatedRecords=0; + Record::Record() { _pData = NULL; _pParent = NULL; _pFltFile = NULL; + + ++s_numAllocatedRecords; + + flt::Registry::instance()->addRecordForFutureDelete(this); } Record::~Record() { + --s_numAllocatedRecords; + if (_pData) ::free(_pData); } diff --git a/src/osgPlugins/flt/Record.h b/src/osgPlugins/flt/Record.h index 954625f9f..35d92702b 100644 --- a/src/osgPlugins/flt/Record.h +++ b/src/osgPlugins/flt/Record.h @@ -33,6 +33,9 @@ class Record : public osg::Referenced { public: + // used for debugging the number of records created and deleted. + static int s_numAllocatedRecords; + Record(); virtual Record* clone() const = 0; diff --git a/src/osgPlugins/flt/Registry.cpp b/src/osgPlugins/flt/Registry.cpp index bcb9c6170..bee52b7e1 100644 --- a/src/osgPlugins/flt/Registry.cpp +++ b/src/osgPlugins/flt/Registry.cpp @@ -1,4 +1,4 @@ - +; #include #include #include @@ -13,7 +13,6 @@ using namespace flt; -// static Registry* Registry::instance() { static Registry s_nodeFactory; @@ -44,6 +43,7 @@ Record* Registry::getPrototype(const int opcode) } + /////////////////////////////////////////////////////////////////// @@ -80,3 +80,10 @@ FltFile* Registry::getFltFile(const std::string& name) return NULL; } +void Registry::clearObjectCache() +{ + _textureMap.clear(); + _fltFileMap.clear(); + + _recordForFutureDeleteList.clear(); +} diff --git a/src/osgPlugins/flt/Registry.h b/src/osgPlugins/flt/Registry.h index d04b74734..836bb9b1b 100644 --- a/src/osgPlugins/flt/Registry.h +++ b/src/osgPlugins/flt/Registry.h @@ -52,12 +52,18 @@ class Registry void addFltFile(const std::string& name, FltFile* file); FltFile* getFltFile(const std::string& name); + + void clearObjectCache(); + void addRecordForFutureDelete(Record* rec) { _recordForFutureDeleteList.push_back(rec); } + private: typedef std::map > RecordProtoMap; typedef std::map > TextureMap; typedef std::map > FltFileMap; + + typedef std::vector > RecordsForFutureDeleteList; /** constructor is private, as its a singleton, preventing construction other than via the instance() method and @@ -67,6 +73,8 @@ class Registry RecordProtoMap _recordProtoMap; TextureMap _textureMap; FltFileMap _fltFileMap; + + RecordsForFutureDeleteList _recordForFutureDeleteList; }; diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index 6337842a9..fed9e910c 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -90,6 +90,7 @@ ConvertFromFLT::ConvertFromFLT() : _unitScale = 1.0; _useTextureAlphaForTranspancyBinning = true; _bHdrRgbMode = false; + _currentLocalVertexPool = 0; } @@ -1891,7 +1892,7 @@ int ConvertFromFLT::addMeshPrimitives ( osg::Group &parent, GeoSetBuilder *, Mes int ConvertFromFLT::visitLocalVertexPool ( GeoSetBuilder *, LocalVertexPoolRecord *rec ) { // Make the given instance the current one. - rec->makeCurrent(); + _currentLocalVertexPool = rec; // We didn't add any vertices. return 0; @@ -1900,11 +1901,22 @@ int ConvertFromFLT::visitLocalVertexPool ( GeoSetBuilder *, LocalVertexPoolRecor void ConvertFromFLT::visitMeshPrimitive ( osg::Group &parent, MeshPrimitiveRecord *mesh ) { - assert ( mesh ); + if ( !mesh ) + { + osg::notify(osg::NOTICE)<<"Warning:ConvertFromFLT::visitMeshPrimitive () mesh is 0, unable to process."< VertexPaletteOffsetMap; VertexPaletteOffsetMap _VertexPaletteOffsetMap; - int _diOpenFlightVersion; - int _diCurrentOffset; - unsigned short _wObjTransparency; - int _nSubfaceLevel; - double _unitScale; - bool _bHdrRgbMode; - osg::Vec4 _faceColor; - bool _useTextureAlphaForTranspancyBinning; + int _diOpenFlightVersion; + int _diCurrentOffset; + unsigned short _wObjTransparency; + int _nSubfaceLevel; + double _unitScale; + bool _bHdrRgbMode; + osg::Vec4 _faceColor; + bool _useTextureAlphaForTranspancyBinning; - osg::Group* _osgParent; + osg::Group* _osgParent; + + LocalVertexPoolRecord* _currentLocalVertexPool; };