Yeah, I've noticed it's wrong only after I stripped it down.
I was using raw pointers, because I tried to hide OSG behind my application
API (so that OSG headers are not required at build time). I didn't know of
d-pointer approach at that time, but now I'm rewriting it with the approach.

Thanks for the feedback.

2013/5/28 Robert Osfield <robert.osfi...@gmail.com>

> Hi Micheal,
>
> On 27 May 2013 17:39, michael kapelko <korn...@gmail.com> wrote:
> > I've stripped down my OSG app to the attached archive. Upon exit there's
> > some threading error, not real crash. But when I make my mCamera
> > reference-count-controlled by osg::ref_ptr my app crashes. So may be I'm
> > having my threading exit hanging due to not using osg::ref_ptr.
>
> I've just looked at your example and it really is convoluted and wrong
> in a number of ways, we aren't talking OSG bugs, we are talking bugs
> in your code.
>
> 1) You have an event handler that "has a" Viewer, and you attach this
> event handler to the Viewer, which takes a reference, via ref_ptr<>,
> to that event handler creating a
>      circular reference.
>
> 2) You take a C pointer to the a Camera that you assign to the viewer,
> which takes a referecence, via ref_ptr<>, so the Viewer takes
> ownership and will destruct it
>      it automatically once the Viewer gets destructed, but... you
> manually call unref() and the event handler to force destruction of
> the viewer's Camera.  This will either
>      cause a dangling pointer in the Viewer or a dangling pointer in
> the event handler, both of which will attempt to unref() it.
>
> The code is crazy.  We have lots of OSG example that show as sensible
> way to use the OSG, none of them have an event handler owning the
> viewer.  Also you'll almost next see examples of code explicitly
> calling unref(), it's only in very rare cases where you might want to
> not use ref_ptr<> and directly call ref() and unref(), and where you
> do you have to be very careful about management of the pointers
> involved.  I almost never write code that explicitly calls ref() and
> unref(), instead ref_ptr<> is my tool, it's robust and easy to use.
>
> I'm guessing you've resorted to using the unref() because you created
> a circular reference without realizing it and tried to hack your way
> out of this design/implementation error.
>
> In you own I strongly recommend that you make the viewer the topmost
> object in the chain, *always* leave the event handler as a child of
> the viewer, and *never* use ref()/unref() - use ref_ptr<> instead.
>
> Robert.
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to