Hello Ku,

In addition to Torben's answer, which was good, I'll add this:

My object is in an osg::ref_ptr, so I tried to .release() it first, it didn't 
quite change anything...

Actually it did: that's what introduced your large memory leak.

ref_ptr::release() tells that ref_ptr to stop managing the ref count of the object it points to (i.e. don't delete it when its ref count falls to 0). This is only useful when you'll be assigning the pointer to another ref_ptr in the near future, otherwise as you've seen it leaks the whole graph which has that pointer as its root...

ref_ptr::release() should really only be used when returning a ref_ptr from a function as a raw pointer, and when you know you'll assign the return value of the function to another ref_ptr. For example:

osg::Node* createSomeSubgraph()
{
  osg::ref_ptr<osg::Group> root = new osg::Group;
  // ... create the subgraph under root
  return root.release();
}

int main(...)
{
  // ...
  osg::ref_ptr<osg::Node> subgraph = createSomeSubgraph();
  // ...
}

Now that I've explained the results you got, back to what you want. As Torben said, just assign some other pointer to your existing ref_ptr. For example:

osg::ref_ptr<osg::Group> root = new osg::Group;

// ... Use root for some time

root = new osg::Group;

At that point, if the pointer in root has a ref count of 1 (i.e. the ref_ptr called "root" is the only one that has that pointer), at the assignment of another pointer, the ref count will fall to 0 and it will be deleted.

If its ref count is not 1, then it won't be deleted after the assignment, and you'll be really happy you used ref_ptrs because other entities using that pointer will happily continue using it until they're done, instead of using a dangling pointer to some memory you would have freed... But you can keep using root without worrying about it, knowing that the subgraph has either been deleted or will be deleted in the near future.

Hope this helps,

J-S
--
______________________________________________________
Jean-Sebastien Guay    [email protected]
                               http://www.cm-labs.com/
                        http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to