Changeset 9549

Show
Ignore:
Timestamp:
01/26/09 16:16:24 (5 years ago)
Author:
robert
Message:

Fixed effective leak in Program::PerContextProgram? caused by previously osg::State keeping a set of std::ref_ptr<Program::PerContextProgram?> without ever pruning this list.
The fix was to convert the osg::State to use C pointers for the set of applied PerContexProgram? objects, and use the osg::Oberver mechanism to avoid dangling pointers for being maintained in osg::State.

Location:
OpenSceneGraph/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/include/osg/Referenced

    r9343 r9549  
    9090 
    9191        /** Add a Observer that is observing this object, notify the Observer when this object gets deleted.*/ 
    92         void addObserver(Observer* observer); 
     92        void addObserver(Observer* observer) const; 
    9393 
    9494        /** Add a Observer that is observing this object, notify the Observer when this object gets deleted.*/ 
    95         void removeObserver(Observer* observer); 
     95        void removeObserver(Observer* observer) const; 
    9696 
    9797    public: 
     
    122122        struct ObserverSetData; 
    123123 
    124         OpenThreads::AtomicPtr _observerSetDataPtr; 
     124        mutable OpenThreads::AtomicPtr _observerSetDataPtr; 
    125125 
    126126        mutable OpenThreads::Atomic     _refCount; 
     
    131131        mutable int                     _refCount; 
    132132         
    133         void*                           _observers; 
     133        mutable void*                   _observers; 
    134134#endif 
    135135}; 
  • OpenSceneGraph/trunk/include/osg/State

    r9447 r9549  
    2020#include <osg/Uniform> 
    2121#include <osg/BufferObject> 
     22#include <osg/observer_ptr> 
    2223 
    2324#include <osg/FrameStamp> 
     
    8586  *   \c captureCurrentState() and \c getModelViewMatrix()). 
    8687  */ 
    87 class OSG_EXPORT State : public Referenced 
     88class OSG_EXPORT State : public Referenced, public Observer 
    8889{ 
    8990    public : 
     
    961962 
    962963 
    963         void setLastAppliedProgramObject(const Program::PerContextProgram* program) { if (_lastAppliedProgramObject!=program) { _lastAppliedProgramObject = program; if (program) _appliedProgramObjectSet.insert(program); } } 
    964         const Program::PerContextProgram* getLastAppliedProgramObject() const { return _lastAppliedProgramObject; } 
     964        inline void setLastAppliedProgramObject(const Program::PerContextProgram* program) 
     965        { 
     966            if (_lastAppliedProgramObject!=program) 
     967            { 
     968                _lastAppliedProgramObject = program;  
     969                if (program && _appliedProgramObjectSet.count(program)==0) 
     970                { 
     971                    _appliedProgramObjectSet.insert(program);  
     972                    program->addObserver(this); 
     973                } 
     974            } 
     975        } 
     976        inline const Program::PerContextProgram* getLastAppliedProgramObject() const { return _lastAppliedProgramObject; } 
    965977 
    966978        inline GLint getUniformLocation( const std::string& name ) const { return _lastAppliedProgramObject ? _lastAppliedProgramObject->getUniformLocation(name) : -1; } 
     
    10651077        void initializeExtensionProcs(); 
    10661078 
     1079        virtual void objectDeleted(void* object); 
     1080 
    10671081    protected: 
    10681082 
     
    12071221        typedef std::vector<ref_ptr<const Matrix> >                     MatrixStack; 
    12081222         
    1209         typedef std::set<osg::ref_ptr<const Program::PerContextProgram> > AppliedProgramObjectSet; 
     1223        typedef std::set<const Program::PerContextProgram* >            AppliedProgramObjectSet; 
    12101224 
    12111225        ModeMap                                                         _modeMap; 
  • OpenSceneGraph/trunk/src/osg/Referenced.cpp

    r9343 r9549  
    310310} 
    311311 
    312 void Referenced::addObserver(Observer* observer) 
     312void Referenced::addObserver(Observer* observer) const 
    313313{ 
    314314#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) 
     
    338338} 
    339339 
    340 void Referenced::removeObserver(Observer* observer) 
     340void Referenced::removeObserver(Observer* observer) const 
    341341{ 
    342342#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) 
  • OpenSceneGraph/trunk/src/osg/State.cpp

    r9469 r9549  
    9191State::~State() 
    9292{ 
     93    for(AppliedProgramObjectSet::iterator itr = _appliedProgramObjectSet.begin(); 
     94        itr != _appliedProgramObjectSet.end(); 
     95        ++itr) 
     96    { 
     97        (*itr)->removeObserver(this); 
     98    } 
     99} 
     100 
     101void State::objectDeleted(void* object) 
     102{ 
     103    const Program::PerContextProgram* ppcp = reinterpret_cast<const Program::PerContextProgram*>(object); 
     104    AppliedProgramObjectSet::iterator itr = _appliedProgramObjectSet.find(ppcp); 
     105    if (itr != _appliedProgramObjectSet.end())  
     106    { 
     107        osg::notify(osg::NOTICE)<<"Removing _appliedProgramObjectSet entry "<<ppcp<<std::endl; 
     108        _appliedProgramObjectSet.erase(itr); 
     109    } 
    93110} 
    94111 
  • OpenSceneGraph/trunk/src/osgWrappers/osg/State.cpp

    r9449 r9549  
    4444        I_DeclaringFile("osg/State"); 
    4545        I_BaseType(osg::Referenced); 
     46        I_BaseType(osg::Observer); 
    4647        I_Constructor0(____State, 
    4748                       "", 
     
    716717                  __void__initializeExtensionProcs, 
    717718                  "Initialize extension used by osg:State. ", 
     719                  ""); 
     720        I_Method1(void, objectDeleted, IN, void *, object, 
     721                  Properties::VIRTUAL, 
     722                  __void__objectDeleted__void_P1, 
     723                  "", 
    718724                  ""); 
    719725