You are correct, the first class contains a bug. If any code is going to
keep a pointer to an osg::Referenced long-term, it must use a ref_ptr (like
in the second class).
 
Using a normal pointer is very bad. It doesn't increment the ref count. As a
result, if the ref count drops to zero, the memory would be deleted and the
first class would be left with a dangling pointer.
 
Thanks for posting this, this is a good example for the book.
   -Paul
 


  _____  

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Nathan Bates
Sent: Tuesday, January 16, 2007 9:11 AM
To: osg users
Subject: RE: [osg-users] ref_ptr and Geode/Geometry


Paul Martz <[EMAIL PROTECTED]> wrote:
>
> >No, that's totally wrong.  The originator of an object
> >    must be the first to encapsulate an object in ref_ptr.
> >
>You're mistaken, my friend. An application can write code that 
>allocates
>something derived from Referenced, but doesn't store it in a ref_ptr;
>instead the app passes it to OSG and OSG is then the first to assign a
>ref_ptr to it. The app has now relinquished control of that memory.

Good point which goes back to the problem of object ownership
and deletion responsibility.

The first class is buggy -- right?
The second class is correct -- right?

Thanks
-- Nathan

class App
{
    App()
    {
        mGeom = new osg::Geometry;
        group->addChild( mGeom );  // -- INVALIDATES mGeom ???  --
    }
    Func()
    {
        mGeom->setVertexArray();  // -- BUG! TOUCHED INVALIDATED MEMBER --
    }
    osg::Geometry*  mGeom;
};

class App
{
    App()
    {
        mGeom = new osg::Geometry;
        group->addChild( mGeom.get() );  // get() for ret_ptr
    }
    Func()
    {
        mGeom->setVertexArray();  // ok -- mGeom still exists because refcnt
> 0
    }
    ref_ptr<osg::Geometry>  mGeom;  // ref_ptr
};





  _____  

Get
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domains/?p
=BESTDEAL> your own web address.
Have a HUGE year through Yahoo!
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domains/?p
=BESTDEAL> Small Business.

_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to