Hello Erlend,

> The tutorials though use "dumb" pointers as in the Basic Geometry example:
> "
> ...
> int main()
> {
>    ...
>    osg::Group* root = new osg::Group();
>    osg::Geode* pyramidGeode = new osg::Geode();
>    osg::Geometry* pyramidGeometry = new osg::Geometry();
> "
> 
> but i guess it's just for simplicity.

Well, since whenever you add almost anything to a Group, Geometry or 
Geode (or their subclasses) it internally stores them as ref_ptrs, it's 
as safe. For example:

osg::Group* group = new osg::Group;
osg::Geode* geode = new osg::Geode;
group->addChild(geode);
osg::Geometry* geometry = new osg::Geometry;
geode->addDrawable(geometry);

Internally, group stores its children as a vector of ref_ptrs, and geode 
stores its drawables as a vector of ref_ptrs (check the source).

> In the guide "A Short Introduction to the Basic Principles of the Open Scene 
> Graph" on the use of ref_ptr, they first create a node using ref pointer, 
> then they add a node using new, and say:
> 
> "
> Line 25 does more or less the same thing as the previous case. The
> difference is that the geode is allocated with new and added as group's
> child in a single line of code. This is quite safe, too, because there are
> not many bad things that can happen in between (after all, there is no
> in between.)
> "
> 
> that may be a little missleading since it's not a smart pointer, and needs to 
> be deleted explicitly.

No. You cannot delete a subclass of Referenced, because the destructor 
is protected. You also cannot allocate one on the stack. So these are 
illegal:

osg::Group group;

// or

osg::Group* group = new osg::Group;
// ...
delete group;

You will get compile errors for these, because the destructor is protected.

If you create a subclass of Referenced with a C pointer and never give 
it to a ref_ptr, it will leak and there's nothing you can do about it. 
But if you give it to for example a group as a child, it will be OK.

But that said, I find it easier to just use ref_ptrs all the time and 
not think about it. For example:

osg::ref_ptr<osg::Group> group = new osg::Group;
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
group->addChild(geode.get());
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geode->addDrawable(geometry.get());

Hope this helps,

J-S
-- 
______________________________________________________
Jean-Sebastien Guay    [EMAIL PROTECTED]
                                http://www.cm-labs.com/
                         http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to