Hi Fred, Returning a C pointer is more flexible, and as long as ref counting is managed cleanly everything works fine, so the ApplicationUsage::instance() code you suggest is perfectly fine, and avoids thrashing the reference count something that is helps on the performance front.
One little change I'm slowly rolling out in the various singletons that the OSG users is that I'm moving across to using the convention: osg::ref_ptr<ApplicationUsage>& ApplicationUsage::instance() { static osg::ref_ptr<ApplicationUsage> s_applicationUsage = new ApplicationUsage; return s_applicationUsage.get(); } The subtle thing here is that the code is passing back a reference to the static ref_ptr<> rather than a ref_ptr<> like in your suggestion. A subtle difference but really important in a couple of ways - first up it avoid the thrashing of the ref count as no temporary ref_ptr<> are passed around, second it allows uses to reassign the singleton's static ref_ptr<> so you can manually delete the singleton or set an custom objects in its place. This trick is only appropriate for static ref_ptr<> object though, I wouldn't advice this a general solution. Robert. On Wed, Dec 3, 2008 at 5:22 PM, Fred <[EMAIL PROTECTED]> wrote: > So when I see things like the following in the OSG code base is it wrong? > > ApplicationUsage* ApplicationUsage::instance() > { > static osg::ref_ptr<ApplicationUsage> s_applicationUsage = new > ApplicationUsage; > return s_applicationUsage.get(); > } > > To me this looks like your first example. > > // DON'T do this. It stores the address as the return value on > // the call stack, but when the grp ref_ptr<> goes out of > // scope, the reference count goes to zero and the memory is > // deleted. The calling function is left with a dangling > // pointer. > osg::Group* createGroup() > { > // Allocate a new Group node. > osg::ref_ptr<osg::Group> grp = new osg::Group; > > // Return the new Group's address. > return grp.get(); > } > > Should it be as in the second example? > > osg::ref_ptr<osg::Group> createGroup() > { > osg::ref_ptr<osg::Group> grp = new osg::Group; > // Return the new Group's address. This stores the Group > // address in a ref_ptr<> and places the ref_ptr<> on the > // call stack as the return value. > return grp.get(); > } > > In other words... > > osg::ref_ptr<osg::ApplicationUsage> ApplicationUsage::instance() > { > static osg::ref_ptr<ApplicationUsage> s_applicationUsage = new > ApplicationUsage; > return s_applicationUsage.get(); > } > _______________________________________________ > 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