Hi Gianni,

I'll add this to Serge's answer:

Is it true that if I declare a pointer as follows:

osg::Group* test = new osg::Group;

I would have a memory leak since I can not call a delete on "test"
object pointer?

You would have a leak if you never add "test" to a container that holds ref_ptrs. For example, if you add "test" as child to another group, or set it as scene data on your viewer, it will be contained in a ref_ptr and will be deleted when the last containing object goes out of scope.

The reason is that Referenced (the base class for ref counted objects, which Group inherits from indirectly) starts its reference count at 0, and every time you give the pointer to your object to a ref_ptr, it increments it. So for example:

main()
{
  osg::Group* group = new osg::Group;    // ref count 0

  osgViewer::Viewer viewer;
  viewer.setSceneData(group);            // ref count 1
}                                        // ref count 0 --> delete
        
There will be no leak there, because at the end of the scope of main(), viewer will be destroyed, which destroys its member variables, one of which is the scene data, which is a ref_ptr. The ref count will fall to 0 and the object to which "group" pointed will be deleted.

But having said that, it's never wrong to put it in a ref_ptr in the first place, and it can be wrong not to (it can lead to a leak if some things don't happen later, or it can lead to an already-deleted object being used if some other ref_ptr deleted it) so it's easier to always use ref_ptrs.

Hope that clears things up,

J-S
--
______________________________________________________
Jean-Sebastien Guay    jean-sebastien.g...@cm-labs.com
                               http://www.cm-labs.com/
                        http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to