What compiler are you using? The snippet doesn't compile for me with gcc 4.1.2.
It's not hard indeed to create cycles, and handling them robustly would mean checking for every reference stored in a Referenced instance if there isn't a cycle. Which is not impossible, but adds a lot of overhead I suspect. See Caveat #4 in http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html Paul Chris Denham wrote: > 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

