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

Reply via email to