Hi Chris, Using ref_ptr's is far cheaper than a garbage collector we don't have (... until ISO C++ 2009 ?). osg::observer_ptr<> could be used to solve the problem, but you can also simply try to avoid circular references... how ? Well, you could try to define an object to be "slave" of another one. For instance, if 'B' is slave, then A has a ref_ptr<> to B, and B has only a raw pointer (A*). In most cases it's enough because the raw pointer is used only when A is allocated.
Sukender PVLE - Lightweight cross-platform game engine - http://pvle.sourceforge.net/ Le Tue, 18 Nov 2008 18:22:13 +0100, Chris Denham <[EMAIL PROTECTED]> a écrit: > I'm probably treading very old ground here to do with circular usage of > ref_ptr, but it's new to me and I couldn't find any references in the mail > archive about users with similar problems. > I have pasted below, a very simple example of my usage of ref_ptr that leaks > memory. > I can see why it leaks and the destructors are never called, but I am > looking for any good tips on how best to guard against such a scenario. > Do I need to use weak ref pointers to implement this scenario properly? Can > that be done with ref_ptr? I could call unref but that seems like a bad > idea. > It seemed such an easy trap to fall into, I wondered why it doesn't crop up > more often? And if it does, how others have dealt with circular problems of > this type in OSG? > Chris Denham > > //------------------------------------------ > #include <osg/ref_ptr> > #include <osg/Referenced> > #include <iostream> > > int main(int argc, char* argv[]) > { > struct B; > > struct A : public osg::Referenced > { > ~A() { std::cout << "A::~A() called" << std::endl; } > osg::ref_ptr<B> b; > }; > > struct B : public osg::Referenced > { > ~B() { std::cout << "B::~B() called" << std::endl; } > osg::ref_ptr<A> a; > }; > > osg::ref_ptr<A> a = new A(); > osg::ref_ptr<B> b = new B(); > > a->b = b; > b->a = a; > > return 0; > > } > > _______________________________________________ > osg-users mailing list > [email protected] > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org _______________________________________________ osg-users mailing list [email protected] http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

