From a098ba5e79e281aa6f5438f943e300eed0691f0b Mon Sep 17 00:00:00 2001 From: timoore Date: Thu, 24 Jan 2008 23:05:18 +0000 Subject: [PATCH] Memory leak fixes from Till Busch --- simgear/scene/material/mat.cxx | 63 +++++++++---------------------- simgear/scene/material/mat.hxx | 14 ++----- simgear/scene/material/matlib.cxx | 16 +------- simgear/scene/material/matlib.hxx | 5 --- 4 files changed, 22 insertions(+), 76 deletions(-) diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index 94da1b5c..7e590d63 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -52,8 +52,6 @@ SG_USING_STD(map); #include "mat.hxx" -static map > _tex_cache; - //////////////////////////////////////////////////////////////////////// // Constructors and destructor. @@ -222,35 +220,24 @@ SGMaterial::init () } } -bool -SGMaterial::load_texture ( int n ) -{ - int i = (n >= 0) ? n : 0 ; - int end = (n >= 0) ? n+1 : _status.size(); - - for (; i < end; i++) - { - if ( !_status[i].texture_loaded ) { - SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " - << _status[i].texture_path ); - assignTexture(_status[i].state.get(), _status[i].texture_path, - wrapu, wrapv, mipmap); - _status[i].texture_loaded = true; - } - } - return true; -} - osg::StateSet * -SGMaterial::get_state (int n) const +SGMaterial::get_state (int n) { if (_status.size() == 0) { SG_LOG( SG_GENERAL, SG_WARN, "No state available."); return NULL; } + + int i = n >= 0 ? n : _current_ptr; + + if(!_status[i].texture_loaded) + { + assignTexture(_status[i].state.get(), _status[i].texture_path, + wrapu, wrapv, mipmap); + _status[i].texture_loaded = true; + } + osg::StateSet *st = _status[i].state.get(); - osg::StateSet *st = (n >= 0) ? _status[n].state.get() - : _status[_current_ptr].state.get(); _current_ptr += 1; if (_current_ptr >= _status.size()) _current_ptr = 0; @@ -279,13 +266,7 @@ SGMaterial::build_state( bool defer_tex_load ) stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); - if ( !defer_tex_load ) { - SG_LOG(SG_INPUT, SG_INFO, " " << _status[i].texture_path ); - assignTexture( stateSet, _status[i].texture_path, wrapu, wrapv); - _status[i].texture_loaded = true; - } else { - _status[i].texture_loaded = false; - } + _status[i].texture_loaded = false; osg::Material* material = new osg::Material; material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); @@ -320,21 +301,11 @@ void SGMaterial::set_state( osg::StateSet *s ) void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu, int _wrapv, int _mipmap ) { - map >::iterator _tex_cache_iter; - _tex_cache_iter = _tex_cache.find(fname); - if (_tex_cache_iter == _tex_cache.end()) - { - osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv, - mipmap ? -1 : 0); - texture->setMaxAnisotropy( SGGetTextureFilter()); - state->setTextureAttributeAndModes(0, texture); - _tex_cache[fname] = texture; - } - else - { - state->setTextureAttributeAndModes(0, _tex_cache_iter->second.get()); - // cout << "Cache hit: " << fname << endl; - } + osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv, + mipmap ? -1 : 0); + texture->setMaxAnisotropy( SGGetTextureFilter()); + state->setTextureAttributeAndModes(0, texture); + osg::TexEnv* texEnv = new osg::TexEnv; texEnv->setMode(osg::TexEnv::MODULATE); state->setTextureAttributeAndModes(0, texEnv); diff --git a/simgear/scene/material/mat.hxx b/simgear/scene/material/mat.hxx index a5d328b0..0a0fbd13 100644 --- a/simgear/scene/material/mat.hxx +++ b/simgear/scene/material/mat.hxx @@ -113,18 +113,10 @@ public: // Public methods. //////////////////////////////////////////////////////////////////// - /** - * Force the texture to load if it hasn't already. - * - * @return true if the texture loaded, false if it was loaded - * already. - */ - bool load_texture (int n = -1); - /** * Get the textured state. */ - osg::StateSet *get_state (int n = -1) const; + osg::StateSet *get_state (int n = -1); /** @@ -331,7 +323,9 @@ public: const SGMaterial* getMaterial() const { return mMaterial; } private: - SGSharedPtr mMaterial; + // this cannot be an SGSharedPtr since that would create a cicrular reference + // making it impossible to ever free the space needed by SGMaterial + const SGMaterial* mMaterial; }; void diff --git a/simgear/scene/material/matlib.cxx b/simgear/scene/material/matlib.cxx index 5bf353cb..ba8ef9ab 100644 --- a/simgear/scene/material/matlib.cxx +++ b/simgear/scene/material/matlib.cxx @@ -168,23 +168,9 @@ SGMaterial *SGMaterialLib::find( const string& material ) { return NULL; } - // Destructor SGMaterialLib::~SGMaterialLib ( void ) { -} - - -// Load one pending "deferred" texture. Return true if a texture -// loaded successfully, false if no pending, or error. -void SGMaterialLib::load_next_deferred() { - // container::iterator it = begin(); - for ( material_map_iterator it = begin(); it != end(); it++ ) { - /* we don't need the key, but here's how we'd get it if we wanted it. */ - // const string &key = it->first; - SGMaterial *slot = it->second; - if (slot->load_texture()) - return; - } + SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size()); } const SGMaterial* diff --git a/simgear/scene/material/matlib.hxx b/simgear/scene/material/matlib.hxx index e7f2ca2a..1407e44f 100644 --- a/simgear/scene/material/matlib.hxx +++ b/simgear/scene/material/matlib.hxx @@ -75,11 +75,6 @@ public: // find a material record by material name SGMaterial *find( const string& material ); - /** - * Load the next deferred texture, if there is any. - */ - void load_next_deferred(); - material_map_iterator begin() { return matlib.begin(); } const_material_map_iterator begin() const { return matlib.begin(); } -- 2.39.5