> 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();
> }

In the code above, s_applicationUsage is a static variable, not a local
variable allocated on the stack, so it does not go out of scope when
instance() returns. This is the classic singleton design pattern.

Contrast this with the code below, in which the grp variable is local,
allocated on the stack. It goes out of scope when createGroup() returns,
thus decrementing the ref count to zero, deallocating the memory, and
returning a dangling pointer.

> 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?

I hope that helps?
   -Paul

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to