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
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to