From 3b5ed812165cc937855050c00a661068ee308b8a Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 19 Jan 2014 16:45:01 +0000 Subject: [PATCH] Crash-fix: make mat-lib reference counted. Make SGReaderWriterOptions own the material lib it's passing to loader code, so it cannot be freed unexpectedly. --- simgear/scene/material/matlib.hxx | 5 ++++- simgear/scene/tgdb/obj.cxx | 6 +++--- simgear/scene/util/SGReaderWriterOptions.hxx | 8 +++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/simgear/scene/material/matlib.hxx b/simgear/scene/material/matlib.hxx index 45db7734..8a298b5f 100644 --- a/simgear/scene/material/matlib.hxx +++ b/simgear/scene/material/matlib.hxx @@ -27,6 +27,7 @@ #include +#include #include #include @@ -41,7 +42,8 @@ namespace simgear { class Effect; } namespace osg { class Geode; } // Material management class -class SGMaterialLib { +class SGMaterialLib : public SGReferenced +{ private: class MatLibPrivate; @@ -99,5 +101,6 @@ public: ~SGMaterialLib ( void ); }; +typedef SGSharedPtr SGMaterialLibPtr; #endif // _MATLIB_HXX diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index b5d6dd71..edb35f26 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -905,7 +905,7 @@ public: // Generate all the lighting objects for the tile. osg::LOD* generateLightingTileObjects() { - SGMaterialLib* matlib = NULL; + SGMaterialLibPtr matlib; if (_options) matlib = _options->getMaterialLib(); @@ -1046,7 +1046,7 @@ public: // Generate all the random forest, objects and buildings for the tile osg::LOD* generateRandomTileObjects() { - SGMaterialLib* matlib = NULL; + SGMaterialLibPtr matlib; bool use_random_objects = false; bool use_random_vegetation = false; bool use_random_buildings = false; @@ -1181,7 +1181,7 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options if (!tile.read_bin(path)) return NULL; - SGMaterialLib* matlib = 0; + SGMaterialLibPtr matlib; if (options) { matlib = options->getMaterialLib(); diff --git a/simgear/scene/util/SGReaderWriterOptions.hxx b/simgear/scene/util/SGReaderWriterOptions.hxx index 2a4b983d..64262135 100644 --- a/simgear/scene/util/SGReaderWriterOptions.hxx +++ b/simgear/scene/util/SGReaderWriterOptions.hxx @@ -22,10 +22,12 @@ #include #include +#include + #include class SGPropertyNode; -class SGMaterialLib; + namespace simgear { @@ -70,7 +72,7 @@ public: void setPropertyNode(const SGSharedPtr& propertyNode) { _propertyNode = propertyNode; } - SGMaterialLib* getMaterialLib() const + SGMaterialLibPtr getMaterialLib() const { return _materialLib; } void setMaterialLib(SGMaterialLib* materialLib) { _materialLib = materialLib; } @@ -100,7 +102,7 @@ protected: private: SGSharedPtr _propertyNode; - SGMaterialLib* _materialLib; + SGSharedPtr _materialLib; osg::Node *(*_load_panel)(SGPropertyNode *); osg::ref_ptr _model_data; bool _instantiateEffects; -- 2.39.5