-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Jean-Sébastien,

Jean-Sébastien Guay wrote:
> Hi Jan,
> 
> The problem is that returning anyRefPtr.get() causes the object to be
> deleted before the value gets to the variable receiving it on the other
> end. That happens because the sequence is actually:
> 
> 1. put the value (the address of the pointer) into a return value register.
> 2. end of function: destroy local variables - here the ref count of the
> ref_ptr goes to 0, hence the object pointed to is destroyed.
> 3. assign return value to variable receiving it in calling function.

Do'h, of course! Yep, that would explain a crash. I have looked at the
function code but didn't really understand what it does in this context.

> So by the time the caller gets the pointer, the object it points to has
> been destroyed. Returning anyRefPtr.release(); is the idiomatic solution
> here - the function that created it does not need it anymore, but the
> local ref_ptr instance should not manage its ref count because it needs
> to be live when it gets to the caller.

In these cases - function creating a pointer that is to be used outside
of it - the more common solution I have seen is to just to create and
work with a raw pointer and return that instead, then assign it into a
new smart pointer outside the function right away (same as the
"osg::ref_ptr<Foo> x = new Foo()" idiom). I didn't see this particular
example before. Well, a nice brown bag moment :)

> I'm actually surprised you don't know about this. The OSG Quick Start
> Guide explains this, and it's been discussed a lot before, and being an
> experienced OSG developer I would have thought you would have run into
> this before.

This problem yes, not this particular solution to it, though. Well, one
learns all the time. Mea culpa :)

Regards,

Jan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org

iD8DBQFKXDbCn11XseNj94gRAngVAJwOzTGG8pdBKF9b++tAe5SdAXyKEQCgq3bK
GKA0b1ALVH5tYytta1jrfnM=
=ldgN
-----END PGP SIGNATURE-----
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org

Reply via email to