[osg-users] Best usage of function: return ref_ptr or standard ptr?
Hi, I have a function which I want to return a group, with two alternatives: Code: osg::ref_ptr osg::Group returnGroup() { osg::ref_ptr osg::Group group = new osg::Group; ... return group; } Code: osg::Group * returnGroup() { osg::Group * group = new osg::Group; ... return group; } Which usage is better if garbage collection is desired? Thank you! Cheers, Juan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45134#45134 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Best usage of function: return ref_ptr or standard ptr?
Hi Juan, Returning a reference counted pointer is in general the safest thing to do as it ensures that the object will get deleted when it goes out of scope. A quick search of the osg-users will reveal a number of threads on the subject alternatively try the link below: http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html K. On 30 January 2012 21:45, Juan Herrera juan...@gmail.com wrote: Hi, I have a function which I want to return a group, with two alternatives: Code: osg::ref_ptr osg::Group returnGroup() { osg::ref_ptr osg::Group group = new osg::Group; ... return group; } Code: osg::Group * returnGroup() { osg::Group * group = new osg::Group; ... return group; } Which usage is better if garbage collection is desired? Thank you! Cheers, Juan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45134#45134 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Best usage of function: return ref_ptr or standard ptr?
Juan Herrera juan...@gmail.com writes: Hi, I have a function which I want to return a group, with two alternatives: Code: osg::ref_ptr osg::Group returnGroup() { osg::ref_ptr osg::Group group = new osg::Group; ... return group; } Code: osg::Group * returnGroup() { osg::Group * group = new osg::Group; ... return group; } Which usage is better if garbage collection is desired? There is no garbage collection in osg. Objects are deleted through reference counting, but that is probably what you mean. I'd go with the second style if nothing in returnGroup stores the pointer. It is certainly safe, if the pointer is assigned to a ref_ptr in another object. In general, use ref_ptr when you need to store something for a longer duration and a plain pointer for short-lived access and creation. Cheers, Philipp Thank you! Cheers, Juan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45134#45134 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org