Using ref_ptr everywhere is a good way to be exception safe because you know
the pointer will be deleted even it it wasn't added to another OSG object.
There is a possible pitfall if you want to return a ref_ptr in a function that
returns a pointer, because the object will be deleted before the function
returns. In that case, you must use the release() method of the ref_ptr object :
osg::Node *createNode() {
osg::ref_ptr<osg::Node> newNode = new osg::Group; // for instance
// here you are exception safe
return newNode.release(); // to avoid returning a pointer to a deleted object
}
osg::ref_ptr<osg::Node> myNode = createNode();
Regards,
-Fred
----- "Glenn Waldron" <[email protected]> a écrit :
> Old, but still relevant, write-up on ref_ptr:
>
>
> http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html
>
>
>
> Glenn Waldron / Pelican Mapping / 703.652.4791 / @glennwaldron
>
>
>
> 2011/3/16 Sergey Polischuk < [email protected] >
>
>
> Hi, Yasser
>
> osg ref_ptr's are intrusive, and object's reference count get's
> incremented every time you create ref_ptr to this object, and it
> decrease every time this ref_ptr destroyed. Inside osg all objects
> that keep pointers on other objects use ref_ptr's for that purpose, so
> if you pass simple pointer to some osg object it'll be refcounted
> correctly. You can not use delete on objects pointer that can be
> wrapped in ref_ptr because they have protected destructor and free
> when there are no more references to them. If you get pointer and need
> to be sure that it will be valid over time, you should create ref_ptr
> out of this pointer (thus increasing ref count) and keep it instead.
> In general if you create osg objects that can be wrapped into
> ref_ptr's you should make ref_ptr out of object's pointer at some
> point (though this can happen inside osg code if you pass pointer to
> some osg object which keeps it with ref_ptr) or they will not be
> destructed.
>
> Cheers,
> Sergey.
>
> 16.03.2011, 23:59, "Yasser Asmi" < [email protected] >:
> > I am new to OSG and while I understand the general reference
> counting rules, I am having some trouble finding all the ref_ptr rules
> we should be following. For instance most examples in OSG do not use
> ref_ptr on objects and also do not free them. Sometime, these objects
> (Geometry, Vec3Array etc) are passed to methods. It is not clear if
> the methods are refcounting etc. Stepping through the source is a way
> to find out but I am looking for general guidance. Is there a resource
> that talks about it?
> >
> > Thank you!
> >
> > Cheers,
> > Yasser
> >
> > ------------------
> > Read this topic online here:
> > http://forum.openscenegraph.org/viewtopic.php?p=37666#37666
> >
> > _______________________________________________
> > 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
>
>
> _______________________________________________
> 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