Hi,
(Let me jump in to take some load of our overloaded project leader  ;-)).

Ivan Bolčina wrote:
I am not sure I understand.
So always when I create osg::* objects (like this array) I also wrap it in ref_ptr?

It's good practice to store any object instance whose class derives from osg::Referenced using a osg::ref_ptr<>. Any of the scenegraph node types (Transform, Group, Geode, etc) are derived from Referenced. Not all things in OSG need to be handled with ref_ptr, e.g. Vec3 isn't derived from Referenced.

Another problem is, why compile of delete vec3array fails...?

Classes that need to be reference-counted have a protected destructor, so you can not create one of these objects on the stack (yes, it's confusing, protected destructor -> no _construction_ on the stack). The reason for this is that reference counting on stack-allocated objects makes no sense at all, as the objects lifetime is coupled to the lifetime of the stack.

>how then ref_ptr removes it,if delete fails?

Deletion of ref-counted objects is done automatically by ref_ptr<> when an object's reference count reaches 0. osg::ref_ptr<> calls Referenced::unref(), which in turn is able to call "delete this;", thereby destructing the object in question.

Second question, if I create hierarhy of nodes, do I:
1. Have to delete root?
2. Dont have to delete anything, since root is pointed on class level with ref_ptr?
3. Alway wrap everything in ref_prt., or just root?
4. On "class level", should I store only ref_ptr to root, or also to child nodes

1, 2. You don't have to explicitly delete anything. Whenever a ref_ptr<> object goes out of scope it decrements the reference count of the object it is pointing to.

3. In general, whenever you want to store a pointer to a reference counted object (one whose class derives from osg::Referenced) use an osg::ref_ptr<>.

4. That depends on what you want to do. If you only want to hold a reference to the root node (and have no need to access any of its children) then there's no need to keep pointers to child nodes. Internally, OSG objects also use ref_ptr<>'s to store references to other nodes.

Thanks!

Hope this helps

Paul


2006/11/22, Robert Osfield <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>:

    HI Gordon,

    On 11/22/06, Gordon Tomlinson <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:
     > I understand the point but the Gents example was not using a Ref
    pointer,
     > hence the way I replied :)

    The best advice was to use ref_ptr<> without a shadow of doubt.

     > But I would also argue that theres nothing wrong in using unref()
    if you
     > know what the life cycle of your object is.

    It is not a robust way to program.  What happens when an exception
    occurs?  Memory leak?  what happens if you move the code around a bit
    and the code path missing a ref/unref?  Calling unref() mannually is a
    *bad* programming practice that one shouldn't get it in.

    ref_ptr<> is your friend, it will help your out immensely, it frees
    you from worry about memory management because it does it so robustly.

    Robert.
    _______________________________________________
    osg-users mailing list
    osg-users@openscenegraph.net <mailto:osg-users@openscenegraph.net>
    http://openscenegraph.net/mailman/listinfo/osg-users
    http://www.openscenegraph.org/



------------------------------------------------------------------------

_______________________________________________
osg-users mailing list
osg-users@openscenegraph.net
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/


--
Paul Melis

VR Specialist,
Center for High-Performance Computing & Visualization,
University of Groningen,
The Netherlands

T: +31 50 363 9298
E: [EMAIL PROTECTED]
W: http://www.rug.nl/rc/hpcv/index
_______________________________________________
osg-users mailing list
osg-users@openscenegraph.net
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to